Imagine you need a sample text for a blog post, a text analysis, or a practice exercise, and you then want to know how often each letter occurs in it. In this tutorial, you solve exactly this problem with a dictionary comprehension. A placeholder text serves as the test text, such as web developers often use from sites like loremipsum.de. At first glance it looks like real running text, but you can ignore its content, because you only care about the characters.
The tutorial lasts about five and a quarter minutes. You will learn the syntax of a dictionary comprehension, understand how it differs from a list comprehension, and see why a small change using a set noticeably streamlines the calculation.
Key Takeaways
Key Takeaways
- A dictionary comprehension uses curly braces and follows the pattern
{key: value for element in sequence}. - The key and the value are separated by a colon, just like in a normal dictionary.
- With
text.count(character)you can determine how often a character occurs in the text. - If you iterate over the entire text, you count characters multiple times. A
set()reduces the characters to unique values and makes the calculation more efficient. - The order of a set is not sorted. The result does contain all counts correctly, but the letters do not appear alphabetically.
Step-by-Step Guide
Step 1: Prepare the Test Text
Start by writing a sample text into a variable. In the video, a placeholder text of about twenty words is copied from loremipsum.de and assigned to the variable mein_text. To keep the text easy to read, you insert line breaks at suitable places. You do not need to understand the content of the text, because the analysis is only interested in the individual characters.

Step 2: Formulate the Goal
Before you write code, define what the end result should be. You want a dictionary in which each character is a key and the number of its occurrences is the value. An entry could therefore look like this: the letter "L" stands for the number of capital L's in the text. This idea helps you build the comprehension step by step later on.

Step 3: The Syntax of the Dictionary Comprehension
The syntax is modeled on the list comprehension. The decisive difference is the type of brackets: a list comprehension uses square brackets, a dictionary comprehension uses curly ones. Inside the brackets, you first write the key, then a colon, and then the value. Next comes the loop with for, which determines which elements are iterated over.
For the characters themselves, you use the text directly. Python regards a string as a sequence of individual characters, so for zeichen in mein_text provides every letter, every space, and every line break one after another.
Step 4: Determine the Value with count()
The key is already defined, namely the current character. Now the value is still missing, that is, the number of occurrences. For this, you use the method count() on the text. With mein_text.count(zeichen), Python returns the frequency of the respective character.
The complete comprehension thus looks like this:
The key is the character, the value is its frequency. The comprehension thus runs through the character list of the text and creates an entry for each character.
Step 5: Output the Result
Then you output the dictionary with print(text_stats). You will then see pairs of character and count. At the beginning, a line break appears, which you can remove if needed. The output shows you, for example, the value one for the capital L, a significantly higher value for the lowercase o, and for spaces the number of gaps between words. The counting therefore works for all characters, not just for letters.

Step 6: Recognizing the Weakness of the First Solution
On closer inspection, a flaw becomes apparent. The loop runs over every occurrence of a character in the text. If an o appears ten times, it is therefore also treated as a key ten times. Each time, count() then calculates the same number again. The result remains correct, but the calculation is unnecessarily costly. With longer texts, this extra work becomes significantly noticeable.
Step 7: Avoiding Repeated Passes with a Set
A set helps here. A set stores each value only once. If you convert the text into a set with set(mein_text), it contains each occurring letter exactly once. So you only change the loop, not the logic of the comprehension:
Now each character is counted only once, which makes the calculation significantly more efficient. The result remains identical, only the path to it is shorter.

Step 8: Understanding the Order in the Result
When you output the new result, you see the same values as before. The count for the capital L or the lowercase o has stayed the same. The difference lies in the order: a set is not sorted, so the characters no longer appear alphabetically. If you need a sorted output, you can subsequently order the dictionary with sorted(). For the pure counting, however, the order is irrelevant.

Step 9: Key and Value at a Glance
Finally, you summarize the structure once more. The key is to the left of the colon and defines the name under which an entry is stored. The value is to the right of the colon and contains the result calculated for that key. The logic is thus the same as with a normal dictionary. The only difference is that key and value are generated in a loop.

Summary – Dictionary Comprehension in Python: Counting Letters Step by Step
You create a dictionary comprehension with curly braces, a key, a colon, a value, and a for loop. In the example, the character serves as the key, and mein_text.count(zeichen) supplies the value. If you let the loop run over set(mein_text), each character is calculated only once. The result is the same, but the code is leaner and faster. Once you have understood this structure, you can apply it to many other tasks, such as counting words or creating frequency distributions.
FAQ
What is the difference between a list comprehension and a dictionary comprehension?A list comprehension uses square brackets and creates a list of values, while a dictionary comprehension uses curly braces and creates key-value pairs.
Why is a set used if the text is already a sequence of characters?A set contains each character only once. As a result, count() is not called multiple times for the same letter, which makes the calculation more efficient.
Does the result stay the same with the set?Yes, the number of occurrences is identical. Only the order in which the characters appear in the dictionary has changed.
How do I sort the result alphabetically?You can perform the sorting with sorted(text_stats.items()), ordering the key-value pairs by the character.
Can I also use the comprehension for words instead of letters?Yes, you simply replace the splitting of the text, for example with mein_text.split(), and then count the words with count().
