I'm a beginner with Python. Let me show you my code below and I'll describe my problem.
random_list=['a','b','c','b','d','m','n','n']
duplicates=[]
for value in random_list:
if random_list.count(value) > 1:
if value is not duplicates:
duplicates. Append(value)
print(duplicates)
Below is what happens when I run the code.
[]
['b']
['b']
['b', 'b']
['b', 'b']
['b', 'b']
['b', 'b', 'n']
['b', 'b', 'n', 'n'
The problem is that it shows me the duplicates however, it shows them too many times. How do I tell Python that I only want to show the duplicates once? Example: ['b','n']. Please note, I'm not trying to eliminate the duplicates in my list, I'm trying to show them.
I've put tried different controls in my loops but they failed. They only showed one duplicate.
source https://stackoverflow.com/questions/77160065/how-to-show-only-the-duplicate-values-from-a-list-in-python
Comments
Post a Comment