The Most Frequent Letters
Practice time!
This time we'll focus on creating custom functions and using a few built-in methods and functions.
The Goal:
🔹Take a list of words
🔹Create a loop for words
🔹Calculate how many times each letter appears
🔹What is the most frequent letter(s) in the word
As always, pause here and try it on your own first. Then come back and let's do it together.
Ready? Set. Code🚀
Sign-Up For Future Updates✨
Be among the first people to hear about
New Python Courses or Useful Resources!
Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!
Sign-Up For Future Updates✨
Be among the first people to hear about
New Python Courses or Useful Resources!
Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!
Sign-Up For Future Updates✨
Be among the first people to hear about
New Python Courses or Useful Resources!
Once there's enough demand I might start a Python Newsletter
with even more Tips and Tricks to help you learn it better!
💻Setup Your Script
Let's brainstorm and create a quick layout for the script. Feel free to comment additional logical steps if you want to.
🔹Define a list of words
🔹Create blank function
🔹Create a loop for words
🔹Execute function on each
random_words = [
"Lighthouse", "Cat", "Umbrella", "Giraffe"
"Success", "Volcano", "Balloon",
"Butterfly", "Mississippi", "Strawberry", "Tnt"
]
def most_frequent_letters(word):
pass
for word in random_words:
results = most_frequent_letters(word)
random_words = [
"Lighthouse", "Cat", "Umbrella", "Giraffe"
"Success", "Volcano", "Balloon",
"Butterfly", "Mississippi", "Strawberry", "Tnt"
]
def most_frequent_letters(word):
pass
for word in random_words:
results = most_frequent_letters(word)
random_words = [
"Lighthouse", "Cat", "Umbrella", "Giraffe"
"Success", "Volcano", "Balloon",
"Butterfly", "Mississippi", "Strawberry", "Tnt"
]
def most_frequent_letters(word):
pass
for word in random_words:
results = most_frequent_letters(word)
For now if you execute nothing is going to happen because our function only has a pass keyword, which means DO NOTHING. It's used as a placeholder.
Now let's add some logic.
Count Letters
Let's start working on a function.
I'm going to add some print statements and then we'll focus on the first step - count letters
def most_frequent_letters(word):
print('-'*50)
print(f'🔎 Checking word: {word}')
for letter in word:
count = word.count(letter)
print(letter, count)
def most_frequent_letters(word):
print('-'*50)
print(f'🔎 Checking word: {word}')
for letter in word:
count = word.count(letter)
print(letter, count)
def most_frequent_letters(word):
print('-'*50)
print(f'🔎 Checking word: {word}')
for letter in word:
count = word.count(letter)
print(letter, count)
Now if you execute, you'll be able to see how many times each letter is used.
💡 Notice that Uppercase and Lowercase letters aren't counted as the same. So, we need to convert all letters into lower() or upper(). We can add this line on the top of function:
💡 Also you can notice that letters are repeating, so we might need to store information to avoid duplicate print statements.
def most_frequent_letters(word):
print('-'*50)
print(f'🔎 Checking word: {word}')
word = word.lower()
dict_count = {}
for letter in word:
if letter not in dict_count:
count = word.count(letter)
dict_count[letter] = count
for letter, count in dict_count.items():
print(f'{letter} used {count} times.')
def most_frequent_letters(word):
print('-'*50)
print(f'🔎 Checking word: {word}')
word = word.lower()
dict_count = {}
for letter in word:
if letter not in dict_count:
count = word.count(letter)
dict_count[letter] = count
for letter, count in dict_count.items():
print(f'{letter} used {count} times.')
def most_frequent_letters(word):
print('-'*50)
print(f'🔎 Checking word: {word}')
word = word.lower()
dict_count = {}
for letter in word:
if letter not in dict_count:
count = word.count(letter)
dict_count[letter] = count
for letter, count in dict_count.items():
print(f'{letter} used {count} times.')
So we've made a few changes:
🔹Made all letters lowercase
🔹Create dict container
🔹Check if letter is already in a dict to avoid duplicates
🔹Add letter to dict with count value
🔹Print Dict
Here's how it looks now:
Find Max Count
Now to find max value we can get a list of all values() and put it inside max() function.
max_count = max(dict_count.values())
max_count = max(dict_count.values())
max_count = max(dict_count.values())
Now, let's iterate over characters in the dict and find all letters that have the most count. And keep in mind that multiple letters can have the same max value, so let's group them in a list.
max_count = max(dict_count.values())
max_letters = []
for k,v in dict_count.items():
if v == max_count:
max_letters.append(k)
print(f'The Most Frequent Letter {max_letters}. Used {max_count} times.')
max_count = max(dict_count.values())
max_letters = []
for k,v in dict_count.items():
if v == max_count:
max_letters.append(k)
print(f'The Most Frequent Letter {max_letters}. Used {max_count} times.')
max_count = max(dict_count.values())
max_letters = []
for k,v in dict_count.items():
if v == max_count:
max_letters.append(k)
print(f'The Most Frequent Letter {max_letters}. Used {max_count} times.')
And here's the final result:
Final Touches
Lastly, we can fix a few minor details in the code.
Firstly, let's return some values from our function in case we'd want to reuse them. Just scroll all the way down of the function and write
return max_letters, max_count
return max_letters, max_count
return max_letters, max_count
💡PS. Notice we're returning 2 values and they are going to be combined into a tuple.
So here's how we'd use these returning values in the main section:
for word in random_words:
results = most_frequent_letters(word)
letters = results[0]
count = results[1]
print(letters, count)
for word in random_words:
results = most_frequent_letters(word)
letters = results[0]
count = results[1]
print(letters, count)
for word in random_words:
results = most_frequent_letters(word)
letters = results[0]
count = results[1]
print(letters, count)
And lastly, we can fix print statement so we display singular/plural ending for:
used 1 time / times . It's just a simple logical statement
for letter,count in dict_count.items():
if count > 1:
print(f'{letter} used {count} times.')
else:
print(f'{letter} used {count} time.')
for letter,count in dict_count.items():
if count > 1:
print(f'{letter} used {count} times.')
else:
print(f'{letter} used {count} time.')
for letter,count in dict_count.items():
if count > 1:
print(f'{letter} used {count} times.')
else:
print(f'{letter} used {count} time.')
💻 Final Code
Now let's put it all together into a single script:
random_words = [
"Lighthouse", "Cat", "Umbrella", "Giraffe"
"Success", "Volcano", "Balloon",
"Butterfly", "Mississippi", "Strawberry", "Tnt"
]
def most_frequent_letters(word):
print('-'*50)
print(f'🔎 Checking word: {word}')
word = word.lower()
dict_count = {}
for letter in word:
if letter not in dict_count:
count = word.count(letter)
dict_count[letter] = count
for letter,count in dict_count.items():
if count > 1:
print(f'{letter} used {count} times.')
else:
print(f'{letter} used {count} time.')
max_count = max(dict_count.values())
max_letters = []
for k,v in dict_count.items():
if v == max_count:
max_letters.append(k)
print(f'The Most Frequent Letter {max_letters}. Used {max_count} times.')
return max_letters, max_count
for word in random_words:
results = most_frequent_letters(word)
random_words = [
"Lighthouse", "Cat", "Umbrella", "Giraffe"
"Success", "Volcano", "Balloon",
"Butterfly", "Mississippi", "Strawberry", "Tnt"
]
def most_frequent_letters(word):
print('-'*50)
print(f'🔎 Checking word: {word}')
word = word.lower()
dict_count = {}
for letter in word:
if letter not in dict_count:
count = word.count(letter)
dict_count[letter] = count
for letter,count in dict_count.items():
if count > 1:
print(f'{letter} used {count} times.')
else:
print(f'{letter} used {count} time.')
max_count = max(dict_count.values())
max_letters = []
for k,v in dict_count.items():
if v == max_count:
max_letters.append(k)
print(f'The Most Frequent Letter {max_letters}. Used {max_count} times.')
return max_letters, max_count
for word in random_words:
results = most_frequent_letters(word)
random_words = [
"Lighthouse", "Cat", "Umbrella", "Giraffe"
"Success", "Volcano", "Balloon",
"Butterfly", "Mississippi", "Strawberry", "Tnt"
]
def most_frequent_letters(word):
print('-'*50)
print(f'🔎 Checking word: {word}')
word = word.lower()
dict_count = {}
for letter in word:
if letter not in dict_count:
count = word.count(letter)
dict_count[letter] = count
for letter,count in dict_count.items():
if count > 1:
print(f'{letter} used {count} times.')
else:
print(f'{letter} used {count} time.')
max_count = max(dict_count.values())
max_letters = []
for k,v in dict_count.items():
if v == max_count:
max_letters.append(k)
print(f'The Most Frequent Letter {max_letters}. Used {max_count} times.')
return max_letters, max_count
for word in random_words:
results = most_frequent_letters(word)
Summary
Great Job! During this lesson:
You created a reusable function.
You practiced loops, dictionaries, and string methods.
You learned how to return multiple values from a function.
And you cleaned up the output for readability.
💡Remember, that there are always many ways to solve the same problem. If your solution looks different that's totally fine as long as it works. Focus on getting from A to B and there might be many journeys to get there.
Happy Coding!
🙋♂️ See you in the next lesson.
- EF
Happy Coding!
🙋♂️ See you in the next lesson.
- EF
Happy Coding!
🙋♂️ See you in the next lesson.
- EF