Learn Python in 30 Days — Day 22: Lists & Dictionary Comprehensions
Day 22: Lists & Dictionary Comprehensions
Welcome to Day 22 of the Learn Python in 30 Days series!
Today we get into one of Python’s cleanest “superpowers”: comprehensions.
These let you build lists, dictionaries, and sets in a single, readable line, perfect for filtering data, transforming it, or generating new structures without writing full loops.
By the end of today, you’ll know how to:
-
Turn long loops into compact one-liners
-
Build new lists from existing data
-
Filter items based on conditions
-
Create dictionaries on the fly
-
Use comprehensions with nested structures
This is the kind of Python feature that instantly makes your code cleaner and easier to read.
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
What Is a Comprehension?
A comprehension is a compact way to create a new list or dictionary by looping over something.
Same result but far more compact.
List Comprehensions
They follow this pattern: -
-
Take each item from a sequence
-
Transform it (or keep it as is)
-
Collect the results into a new list
It’s especially useful when you need to convert or reshape data.
Example: Convert temperatures from °C → °F
Adding Filtering Conditions
You can attach an if statement at the end to keep only the items that match your rule.
This turns a comprehension into a combined filter + transform tool.
It prevents extra loops or awkward logic, and keeps your intent clear:
-
Iterate
-
Check a condition
-
Keep the values that pass
When working with text, sensor readings, user input, or API data, this is incredibly handy.
Example: Keep only even numbers
Example: Extract short words
Dictionary Comprehensions
Dictionary comprehensions work just like list comprehensions, but they build key/value mappings.
Pattern:
This is perfect when you want to restructure data, turn lists into dictionaries, or transform existing dictionaries.
Instead of manually creating a dict and assigning inside a loop, you express the whole transformation in one place.
Example: Map a list of names to their lengths
Example: Count character frequency
Transforming Dictionaries
Sometimes you already have a dictionary but want to modify the values, for example:
-
adjust prices
-
convert units
-
clean up strings
-
normalise data
Dictionary comprehensions let you “remap” a dictionary in one step by looping over .items().
It’s cleaner and reduces errors from accidentally mutating data in place.
Example: Convert all prices with tax added
Try it yourself, hopefully you'll see something like that shown below: -You can also download this example from my GitHub here and run it yourself.Comprehensions with Conditions in the Output
Instead of filtering items out, you can replace them using a conditional expression:
This adds logic to the value-creation part of the comprehension.
It’s great for labelling data, cleaning text, or generating UI elements.
For example:
-
Tag numbers as "even"/"odd"
-
Flag values that exceed a threshold
-
Convert only some items to uppercase
You keep every element but transform them differently.
Example: Categorise numbers
Try it yourself, hopefully you'll see something like that shown below: -You can also download this example from my GitHub here and run it yourself.Nested Comprehension
Nested comprehensions let you flatten nested lists or generate combinations.
It’s basically a compact version of writing two for-loops:
Great when dealing with nested lists or data structures from Day 13.
Flattening is a common pattern when working with:
-
matrices
-
grid-based games
-
pixel arrays
-
CSV data
-
embedded dictionaries/lists
The nested comprehension makes this clean and readable.
Example: Flatten a 2D list
Mini-Project: Keyword Highlighter
This small project shows how comprehensions can combine:
-
conditions
-
string methods
-
transforming values
-
looping
We take a list of words and highlight only the ones containing a chosen letter.
It mimics real-world tasks like:
-
searching logs
-
highlighting keywords in text
-
filtering filenames
-
analysing Python code tokens
Take a list of words and highlight only the ones that match a condition.
Output example:
This shows how filtering + transforming can combine into one clean comprehension.
Try it yourself, hopefully you'll see something like that shown below: -
You can also download this example from my GitHub here and run it yourself.Next Up — Day 23 – Working with JSON
Tomorrow, you’ll look into JSON and reading / writing structured data in this format.
All example files for this series are available on my GitHub: Learn-Python-in-30-Days
You can see the full series here Learn Python in 30 Days series!
Hope you have enjoyed this post, thanks Matty







Comments
Post a Comment