+ 3
How to compare all the elements of a list with each other?
For example. I have list List = [1,5,7,87,9,0] And I should compare all the elements and find out how many similar items are there
10 Réponses
+ 7
Very good one, Asman-H, but error in line 3 of the code due to the typo "duples" instead of "dupls"
+ 6
If you want just find out, does the list have duplicate items, use set:
List = [1, 5, 7, 87, 9, 0]
List_without_dupls = set(List)
if len(List) == len(List_without_duples):
print('No duplicates!')
else:
print('There are duplicates!')
How many:
my_list = [1, 2, 3, 4, 1, 1, 2, 2]
my_list_set = set(my_list)
dupls_count = len(my_list) - len(my_list_set)
print('List has', dupls_count, 'duplicates!')
+ 3
You can also use the count method.
I like this pattern:
for item in sorted(set(that_list)):
print(f'{item}: {that_list.count(item)}')
+ 2
Title of the code says it all...
https://code.sololearn.com/czQkcFdB8g0D/?ref=app
+ 1
I must know which elements are equal, (particularly)