Max Andersson
Max Andersson

Reputation: 135

Comparing a set and list and adding the duplicates to a new list Python

I'm trying to compare one list and one set using two for loops, what I find weird is that sometimes 'O' is in the output and sometimes 'L' is but never both. Does it have to do with set being unsorted? Also is there a better way of getting the duplicates of a list? Preferably the unique duplicates. Any help is appreciated!

def duplicate_count(text):
 charList = [*text]
 charSet = set(charList)

  for s in charSet:
   for f in charList:
     if s == f: charList.remove(s)  

 return charList

print(duplicate_count("HELOLO"))

Upvotes: 0

Views: 36

Answers (2)

Gábor Fekete
Gábor Fekete

Reputation: 1358

def duplicates(text):
    text = text.lower()
    return [c for c in dict.fromkeys(text) if text.count(c) > 1]

print(duplicates('HELOLO'))
print(duplicates('heoLLo'))

Output:

['l', 'o']
['o', 'l']

Using set is okay if you don't care about the order of the duplicates present in the text, I used dict.fromkeys so that the order is kept. The text is converted to lowercase so it will be case-insensitive.

Upvotes: 0

Cory Kramer
Cory Kramer

Reputation: 117856

You are removing from charList while you are iterating it, which will mess up your iteration.

I would use collections.Counter to count your characters, then you can look for any characters that have a count greater than 1, meaning there are duplicates

>>> from collections import Counter
>>> s = 'HELOLO'
>>> c = Counter(s)
>>> c
Counter({'L': 2, 'O': 2, 'H': 1, 'E': 1})
>>> sum(1 for i in c if c[i] > 1)
2

or if you want a list of the letters that are duplicated

>>> [k for k,v in c.items() if v > 1]
['L', 'O']

Upvotes: 3

Related Questions