finding the count of repetition of every element in a list | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

finding the count of repetition of every element in a list

Hi, I need a code that checks every element in a list and assigns a count of repetition to that element. for example: list1 = ['i' , 'be' , 'then' , 'i' , 'i' , 'then' ] and the output would be something like this: count of "i" is 3 count of "then" is 2 count of "be" is 1 #note that output will show count of all of them at once

1st Sep 2022, 8:12 AM
Alireza Azizzadeh
Alireza Azizzadeh - avatar
2 Answers
+ 3
Hi! Please, show your attempt.
1st Sep 2022, 8:13 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 3
Alireza Azizzadeh 1. Create an empty dictionary 2. Iterate through your list if item not in dict, then add item as key : value =1. if item already in dict, then value +=1 When you have finished iterating, you will have a dict containing words : occurences print them out
1st Sep 2022, 10:48 AM
Rik Wittkopp
Rik Wittkopp - avatar