Master List and Dict Comprehension with Python

How to use List and Dict Comprehension in Python

What is List Comprehension in Python? You will learn how to make List Comprehension and show how this also works with dictionaries, called Dict Comprehension. Then show how Dict Comprehension can be used for frequency count.

Step 1: What is List Comprehension in Python?

List Comprehension is a syntactic construct available in some programming languages for creating a list based on existing lists.

Wikipedia.org

It is easiest to demonstrate how to create it in Python (see more about lists and about foo-loops).

my_list = [i for i in range(10)]
print(my_list)

Will result in.

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Here you use the range(10) construct, which gives a sequence from 0 to 9 that can be used in the Comprehension construct.

A more direct example is given here.

my_new_list = [i*i for i in my_list]
print(my_new_list)

Which results in the following.

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

Step 2: List Comprehension with if statements

Let’s get straight to it (see more about if-statements).

my_list = [i for i in range(10) if i % 2 == 0]
print(my_list)

This will result in.

[0, 2, 4, 6, 8]

Also, you can make List Comprehension with if-else-statements.

my_list = [i if i % 2 else -i for i in range(10)]
print(my_list)

This results in.

[0, 1, -2, 3, -4, 5, -6, 7, -8, 9]

Step 3: Dict Comprehension in Python

Let’s dive straight into it (see more about dict).

my_list = [i for i in range(10)]
my_dict = {i: i*i for i in my_list}
print(my_dict)

Which results in.

{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}

Notice, that the lists do not need to contain integers or floats. The constructs also work with any other values.

Step 4: Frequency Count using Dict Comprehension

This is amazing.

text = 'aabbccccc'
freq = {c: text.count(c) for c in text}
print(freq)

Which results in.

{'a': 2, 'b': 2, 'c': 5}

Notice, that here we will recount each instance of the letters. To avoid that you can do as follows.

text = 'aabbccccc'
freq = {c: text.count(c) for c in set(text)}
print(freq)

Which results in the same output. The difference is that you only count for each letter once.

Step 5: Want more?

I am happy you asked. Now you have learned about List and Dict Comprehension in Python.

If this is something you like and you want to get started with Python, then this is part of an 8 hours FREE video course with full explanations, projects on each level, and guided solutions.

The course is structured with the following resources to improve your learning experience.

  • 17 video lessons teaching you everything you need to know to get started with Python.
  • 34 Jupyter Notebooks with lesson code and projects.
  • 2 FREE eBooks to support your Python learning.

See the full FREE course page here.

Learn Python

Learn Python A BEGINNERS GUIDE TO PYTHON

  • 70 pages to get you started on your journey to master Python.
  • How to install your setup with Anaconda.
  • Written description and introduction to all concepts.
  • Jupyter Notebooks prepared for 17 projects.

Python 101: A CRASH COURSE

  1. How to get started with this 8 hours Python 101: A CRASH COURSE.
  2. Best practices for learning Python.
  3. How to download the material to follow along and create projects.
  4. A chapter for each lesson with a descriptioncode snippets for easy reference, and links to a lesson video.

Expert Data Science Blueprint

Expert Data Science Blueprint

  • Master the Data Science Workflow for actionable data insights.
  • How to download the material to follow along and create projects.
  • A chapter to each lesson with a Description, Learning Objective, and link to the lesson video.

Machine Learning

Machine Learning – The Simple Path to Mastery

  • How to get started with Machine Learning.
  • How to download the material to follow along and make the projects.
  • One chapter for each lesson with a Description, Learning Objectives, and link to the lesson video.

2 thoughts on “Master List and Dict Comprehension with Python”

Leave a Comment