Set Cookie unSet Cookie
Connecting Friends and FliXXX to Keep You Cumming Back!

HOW TO’s .. Print Duplicates

Vote DownVote Up (No Ratings Yet)
Loading...

Certainly! Let’s find those duplicates. Here are a few ways to identify duplicate elements in a list:

  1. Using Naive Method (Nested Loops):
    • You can use two nested loops to compare each element with every other element in the list. If any pair of elements match, they are duplicates.
    • Example: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, 14
  2. Using a Set:
    • Create an empty set to store duplicate elements.
    • Loop through the list and add elements to the set if their count in the list is greater than 1.
    • Example:my_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}
  3. Using a Single Loop and count() Method:
    • Loop through the list and check if each element appears more than once.
    • If so, add it to another list (avoiding duplicates in the new list).
    • Example: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]
  4. Using Counter() Method:
    • The Counter() method counts the occurrences of each element in the list.
    • Use a list comprehension to extract elements with a count greater than 1.
    • Example: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! 😊


Featured Partner

Featured Partner

Show Categories
Show Tags
My Favorites
Categories: Blog +
Tags: +



Home
Friends
FliXXX
Live CAMS
1MODELS
FANS