I wrote this blog post, but received some feedback that there are better ways to handle the situation. I'd like to get some more opinions. Here is the topic:
Given a list of letters, I want to find the most commonly seen one.
# create the list of letters
my_list = ['b', 'b', 'b', 'c', 'a', 'b']
# simple sentence to make all more "user friendly"
sentence = '\nLetter which repeat the most is: '
# 3 lists declared globally which will store our values
a_list = []
b_list = []
c_list = []
# for loop which control the letters in the list and append them to the respective list
for v in my_list:
if v == 'a':
a_list.append(v)
elif v == 'b':
b_list.append(v)
elif v == 'c':
c_list.append(v)
# create a variable which store the name of the letter and how many times appear in the main list
A = a_list[0], len(a_list)
B = b_list[0], len(b_list)
C = c_list[0], len(c_list)
# variable which store length values
total_list = A[1], B[1], C[1]
# find the max value
largest = max(total_list)
# in this loop we are going to compare each value from total_list to the largest one
for i, val in enumerate(total_list):
if total_list[i] == largest:
x = total_list[i] # assign to "x" this value
# do a check for all the values
# if the condition is satisfied, then print the sentence and letter (to uppercase)
if x == A[1]:
print(sentence + str(A[0]).upper())
elif x == B[1]:
print(sentence + str(B[0]).upper())
elif x == C[1]:
print(sentence + str(C[0]).upper())
I've heard that collections.Counter
could be used for this, but I'm not sure that the output will be the same. I might be able to use it to replace some of my code above, but will I end up with the same result?