Certainly! Let’s find those duplicates. Here are a few ways to identify duplicate elements in a list:
my_list = [10, 12, 14, 10, 16, 14, 18] for i in range(len(my_list)): for j in range(i + 1, len(my_list)): if my_list[i] == my_list[j]: print(my_list[i])
Output: 10, 14my_list = [10, 12, 14, 10, 16, 14, 18] duplicate = set() for num in my_list: if my_list.count(num) > 1: duplicate.add(num) print(duplicate)
Output: {10, 14}count()
Method:
my_list = [10, 12, 14, 10, 16, 14, 18] duplicate = [] for num in my_list: if my_list.count(num) > 1 and num not in duplicate: duplicate.append(num) print(duplicate)
Output: [10, 14]Counter()
Method:
Counter()
method counts the occurrences of each element in the list.from collections import Counter my_list = [10, 12, 14, 10, 16, 14, 18] duplicates = [item for item, count in Counter(my_list).items() if count > 1] print(duplicates)
Output: [10, 14]Feel free to choose the method that suits your needs! 😊