How to compare all the elements of a list with each other? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

12th Nov 2019, 3:10 PM
Марсель Халимов
8 Answers
+ 7
Very good one, Asman-H, but error in line 3 of the code due to the typo "duples" instead of "dupls"
13th Nov 2019, 12:13 AM
ProfOduola🙏🇳🇬
ProfOduola🙏🇳🇬 - avatar
13th Nov 2019, 12:18 AM
ProfOduola🙏🇳🇬
ProfOduola🙏🇳🇬 - avatar
+ 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!')
12th Nov 2019, 3:24 PM
Asman-H
Asman-H - avatar
+ 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)}')
12th Nov 2019, 3:32 PM
HonFu
HonFu - avatar
12th Nov 2019, 5:20 PM
Bilbo Baggins
Bilbo Baggins - avatar
+ 2
Title of the code says it all... https://code.sololearn.com/czQkcFdB8g0D/?ref=app
13th Nov 2019, 11:28 PM
Thoq!
Thoq! - avatar
+ 1
I must know which elements are equal, (particularly)
12th Nov 2019, 3:31 PM
Марсель Халимов
12th Nov 2019, 6:47 PM
Seb TheS
Seb TheS - avatar