How to Find the Most Common Element in a Python String

5/5 - (4 votes)

Problem Formulation and Solution Overview

This article will show you how to find the most common element in a Python string.

To make it more interesting, we have the following running scenario:

Sven, a Photographer, turned Python Coder, was called for an interview at Edge IT. Sven thought the interview was going well until he was given the following problem to solve!


πŸ’¬ Question: How would we write code to find the most common element in a string?

This can be accomplished by using one of the following options:


Method 1: Use Collections.Counter

This example uses the collections library that provides container types allowing the coder to easily store and access data values. The Counter() function keeps a running tally of each element’s count.

import collections

slogan = 'It is not a bug it is an undocumented feature.'
print(collections.Counter(slogan).most_common(1))

The above code imports Python’s built-in collections library and saves a string to the variable slogan.

Then, the collections.Counter() is called and passed the variable slogan as an argument.

Next, most_common (a counter tool for quick tallies) is appended to slogan and is passed an argument of 1. This indicates to return the highest value.

A List of Tuples containing the character with the highest tally and the associated number returns and is output to the terminal.

In this case, the space character was the highest, with 9 occurrences.

[(' ', 9)]

To break the Tuple out of the List, append [0] to the end.

print(collections.Counter(slogan).most_common(1)[0])

This will result in the following output.

(' ', 9)
The Ultimate Guide to Python Tuples | Intro and Theoretical Basics | Part 1/7

Method 2: Use For loop and max()

This example uses a for loop and calls the max() function to determine which character occurs most often in a string.

slogan = 'We Debug Every Software Bug'
common = {}

for i in slogan.lower():
    if i in common:
        common[i] += 1
    else:
        common[i] = 1
most_common = max(common, key=common.get)
print (f'The maximum character is: {most_common}')

The above code creates a string and saves it to the variable slogan. Then, an empty Dictionary is created and saved to the variable common.

Next, a for loop is instantiated to iterate through each element (character) in slogan in lowercase (using lower()).

If the element (character) was previously found, the count increases by 1. If this is the first time the element (character) is found, the initial value is set to 1.

Then, the max() function is called and passed common, retrieving the key with the highest tally. The results save to most_common.

If common was output to the terminal, the following Dictionary would display.

{'w': 2, 'e': 5, ' ': 4, 'd': 1, 'b': 2, 'u': 2, 'g': 2, 'v': 1, 'r': 2, 'y': 1, 's': 1, 'o': 1, 'f': 1, 't': 1, 'a': 1}

If most_common was output to the terminal, the following character would display.

The maximum character is: e

This is correct! The character e can be found 5 times in the string slogan.

πŸ’‘Note: If we did not convert slogan to lowercase before running this code, the result would be different as E and e do not equate to the same value.

Python max() β€” A Simple Illustrated Guide

Method 3: Use For loop and count()

This example uses a for loop and calls the count() function to determine which character occurs most often in a string.

slogan = 'Every bug needs help.'.lower()
rcount = 1

for c in slogan:
    char_count = slogan.count(c)
    if char_count > rcount:
        rcount = char_count
        most_common = c
print(most_common, rcount)

The above code creates a string, converts it to lowercase using lower() and saves it to the variable slogan. Then, a counter variable is created, rcount and assigned a value of 1.

Next, a for loop is instantiated to iterate through each element (character) in slogan checking the current element (character) to see how many times it has occurred thus far in the iteration. The associated variables are updated accordingly.

Upon completion of the iteration, the most_common character and its associated count are output to the terminal.

e 5
Python String Methods [Ultimate Guide]

Method 4: Use max() and a lambda

This example uses max() and a lambda to determine which character occurs most often in a string.

def most_common(text):
    common = [(c, text.count(c)) for c in set(text)]
    return max(common, key=lambda x: x[1])[0]

slogan = 'Software Mechanics'
print(most_common(slogan))

The above code defines a function most_common. This function accepts 1 argument, a string (text).

Let’s start at the bottom where a string is declared and saved to slogan. Then, the above function is called and passed this string (most_common(slogan)).

Inside the function at the top, List Comprehension is used to iterate through each element (character) of the passed string using the set() method. The results save to common.

If common was output to the terminal, the following would display.

[('c', 2), ('w', 1), (' ', 1), ('n', 1), ('s', 2), ('a', 2), ('m', 1), ('r', 1), ('h', 1), ('e', 2), ('i', 1), ('t', 1), ('f', 1), ('o', 1)]

The following outputs the contents of most_common to the terminal.

c
A Simple Introduction to List Comprehension in Python

Bonus: Determine Most Common Element ignoring Spaces

All the above examples show you various ways to determine the most common element in a string. However, what if you don’t want to count the number of times a space character appears?

slogan = 'Amazing Software Mechanics Always Fixing Bugs'.lower()
common = {}

for c in slogan:
    if c == ' ':
        continue
    elif c in common:
        common[c] += 1 
    else:
        common[c] = 1

most_common = sorted(common.items(), key=lambda k: k[1], reverse=True) 
print(most_common[0])!

🌟Finxter Challenge
Modify the code to ignore other characters!


Summary

This article showed you five (5) ways to find the most common element of a string. These examples should provide you with enough information to select the one that best meets your coding requirements.

Good Luck & Happy Coding!


Programming Humor