How to Write a List Comprehension in Python

How to Write a List Comprehension in Python

<strong>Originally published by </strong>&nbsp;<a href="" target="_blank">JEREMY GRIFSKI</a><strong> </strong><em>at&nbsp;</em><a href="" target="_blank"></a>

Welcome back to yet another post in the How to Python series. This time I’m looking to step back a little bit to talk about one of Python’s builtin features called the list comprehension. While we’ve used them a few times in the series, I never thought to really explain them until now.

Table of Contents Problem Introduction

Unlike other articles in this series, there’s not exactly a concrete problem we’re trying to solve in this article. Instead, the goal is to understand the list comprehension syntax:

nums = [2, 6, 10, -4]
negative_nums = [x for x in nums if x < 0]

What is this bizarre syntax, and how does it work? That’s the goal of the article today. In particular, we’ll look at a few scenarios where a list comprehension is useful such as:

  • Duplicating a list
  • Modifying a list
  • Filtering a list
  • Filtering and modifying a list
  • Generate all pairs from two lists
  • Duplicating nested lists

If you know of anything else we can do with a list comprehension, let me know!


Before we can dive into the solutions, let’s talk about the syntax a bit. Here’s my best attempt at illustrating the concept:

output = [expression(item) for item in some_list]

At the most basic level, we can construct a list comprehension that iterates over each item in some list, performs some expression on that item, and places that new item in an output list. Or as a loop:

output = []
for item in some_list:

Of course, we can do a lot more than just create a list from some other list with a list comprehension. In the following subsections, we’ll take a look at a few examples.

Duplicate a List

Perhaps the simplest use of a list comprehension is duplicating another list:

my_list = [2, 5, -4, 6]
output = [item for item in my_list]  # [2, 5, -4, 6]

In this case, output will be equivalent to my_list. For completeness, here’s the same solution as a loop:

my_list = [2, 5, -4, 6]
output = []
for item in my_list:

As we can see, the list comprehension is significantly more concise. In either case, we will only perform a shallow copy—meaning items in the new list may point to the same items in the old list—so it’s a good idea to only use this syntax for copying lists of immutable values like numbers.

Modify a List*

Now that we know how to duplicate a list, let’s try modifying the items before we add them to the output list:

my_list = [2, 5, -4, 6]
output = [2 * item for item in my_list]  # [4, 10, -8, 12]

Instead of copying the original list directly, we modified each item by multiplying it by two before storing it in the new list. As a result, we end up with a list where each term is twice as big as it was in the original list. Here’s the same concept using a loop:

my_list = [2, 5, -4, 6]
output = []
for item in my_list:
  output.append(item * 2)

To be clear, as the asterisk probably hints, we didn’t actually change the original list. Instead, we created a completely new list with the items doubled.

If my_list contained objects or some other mutable data type like a list, there would be nothing stopping us from modifying them. Of course, that’s considered bad practice, so I neglected to share an example on the off chance that someone haphazardly copies it into a production system.

Filter a List

While duplicating and modifying lists is fun, sometimes it’s helpful to be able to filter a list:

my_list = [2, 5, -4, 6]
output = [item for item in my_list if item < 0]  # [-4]

In this case, we’ve added a new expression to the rightmost portion of the list comprehension that reads: if item < 0. Of course, the loop equivalent might look something like the following:

my_list = [2, 5, -4, 6]
output = []
for item in my_list:
  if item < 0:

In other words, for each item in the list, only consider it if it’s less than zero. If it is, dump it to the new list. As a result, we end up with a list that only contains negative values.

Filter and Modify a List

Naturally, we can both modify and filter a list at the same time by combining the syntax:

my_list = [2, 5, -4, 6]
output = [2 * item for item in my_list if item < 0]  # [-8]

In this case, we’ve decided to double all negative values before dumping the results to a list. Once again, the same syntax as a loop might look something like:

my_list = [2, 5, -4, 6]
output = []
for item in my_list:
  if item < 0:
    output.append(item * 2)

As a result, the output list only contains -8. Once again, it’s important to mention that we didn’t actually modify the original list.

Generate All Pairs from Two Lists

Now, we’re starting to get into some of the more advanced features of list comprehensions. In particular, we’re looking to generate pairs of values between two lists:

# [(1, 2), (1, 4), (1, 6), (3, 2), (3, 4), (3, 6), (5, 2), (5, 4), (5, 6)]
output = [(a, b) for a in (1, 3, 5) for b in (2, 4, 6)]

Here, we’ve created a list that contains all combinations of pairs from two lists. As usual, we can implement the same thing with the following set of loops:

output = []
for a in (1, 3, 5):
  for b in (2, 4, 6):
    output.append((a, b))

If we wanted to make things more interesting, we could apply some filtering:

# [(3, 2), (5, 2), (5, 4)]
output = [(a, b) for a in (1, 3, 5) for b in (2, 4, 6) if a > b]

In this case, we only generate a pair if the number from the first list is larger than the number from the second list.

Duplicate Nested Lists

With the shallow copy example mentioned earlier, we’re not able to duplicate nested lists such as two-dimensional matrices. To do that, we can leverage nested list comprehensions:

my_list = [[1, 2], [3, 4]]
output = [[item for item in sub_list] for sub_list in my_list]
print(output) # Prints [[1, 2], [3, 4]]

Instead of performing a surface-level copy, we retrieve each list and copy them using the same comprehension from before. As you can probably imagine, we could abstract this concept into a recursive function which performs a list comprehension on every dimension of the matrix:

def deep_copy(to_copy):
  if type(to_copy) is list:
    return [deep_copy(item) for item in to_copy]
    return to_copy

How cool is that? Of course, if you have anything other than numbers or strings at the deepest levels of your matrix, you’ll have to handle the rest of the cloning process yourself.

A Little Recap

As always, here is a giant dump of all the examples covered in this article with comments briefly explaining each snippet. Feel free to grab what you need and go!

# Define a generic 1D list of constants
my_list = [2, 5, -4, 6]
# Duplicate a 1D list of constants
[item for item in my_list]
# Duplicate and scale a 1D list of constants
[2 * item for item in my_list]
# Duplicate and filter out non-negatives from 1D list of constants
[item for item in my_list if item < 0]
# Duplicate, filter, and scale a 1D list of constants
[2 * item for item in my_list if item < 0]
# Generate all possible pairs from two lists
[(a, b) for a in (1, 3, 5) for b in (2, 4, 6)]
# Redefine list of contents to be 2D
my_list = [[1, 2], [3, 4]]
# Duplicate a 2D list
[[item for item in sub_list] for sub_list in my_list]
# Duplicate an n-dimensional list
def deep_copy(to_copy):
  if type(to_copy) is list:
    return [deep_copy(item) for item in to_copy]
    return to_copy

I hope you had as much fun reading through this article on list comprehensions as I did writing it. I think at this point in the series I’m going to start exploring basic concepts like this and stretching them to their limits. Do you have a Python concept that you’d like explored? Let me know!

In the meantime, why not check out some of these other awesome Python articles:

And, if you’re feeling extra generous, make your way over to the members page and take a look at your options. You’re welcome to try before you buy. That’s why there’s a free option. At any rate, thanks again for the support. Come back soon!

Python GUI Programming Projects using Tkinter and Python 3

Python GUI Programming Projects using Tkinter and Python 3

Python GUI Programming Projects using Tkinter and Python 3

Learn Hands-On Python Programming By Creating Projects, GUIs and Graphics

Python is a dynamic modern object -oriented programming language
It is easy to learn and can be used to do a lot of things both big and small
Python is what is referred to as a high level language
Python is used in the industry for things like embedded software, web development, desktop applications, and even mobile apps!
SQL-Lite allows your applications to become even more powerful by storing, retrieving, and filtering through large data sets easily
If you want to learn to code, Python GUIs are the best way to start!

I designed this programming course to be easily understood by absolute beginners and young people. We start with basic Python programming concepts. Reinforce the same by developing Project and GUIs.

Why Python?

The Python coding language integrates well with other platforms – and runs on virtually all modern devices. If you’re new to coding, you can easily learn the basics in this fast and powerful coding environment. If you have experience with other computer languages, you’ll find Python simple and straightforward. This OSI-approved open-source language allows free use and distribution – even commercial distribution.

When and how do I start a career as a Python programmer?

In an independent third party survey, it has been revealed that the Python programming language is currently the most popular language for data scientists worldwide. This claim is substantiated by the Institute of Electrical and Electronic Engineers, which tracks programming languages by popularity. According to them, Python is the second most popular programming language this year for development on the web after Java.

Python Job Profiles
Software Engineer
Research Analyst
Data Analyst
Data Scientist
Software Developer
Python Salary

The median total pay for Python jobs in California, United States is $74,410, for a professional with one year of experience
Below are graphs depicting average Python salary by city
The first chart depicts average salary for a Python professional with one year of experience and the second chart depicts the average salaries by years of experience
Who Uses Python?

This course gives you a solid set of skills in one of today’s top programming languages. Today’s biggest companies (and smartest startups) use Python, including Google, Facebook, Instagram, Amazon, IBM, and NASA. Python is increasingly being used for scientific computations and data analysis
Take this course today and learn the skills you need to rub shoulders with today’s tech industry giants. Have fun, create and control intriguing and interactive Python GUIs, and enjoy a bright future! Best of Luck
Who is the target audience?

Anyone who wants to learn to code
For Complete Programming Beginners
For People New to Python
This course was designed for students with little to no programming experience
People interested in building Projects
Anyone looking to start with Python GUI development
Basic knowledge
Access to a computer
Download Python (FREE)
Should have an interest in programming
Interest in learning Python programming
Install Python 3.6 on your computer
What will you learn
Build Python Graphical User Interfaces(GUI) with Tkinter
Be able to use the in-built Python modules for their own projects
Use programming fundamentals to build a calculator
Use advanced Python concepts to code
Build Your GUI in Python programming
Use programming fundamentals to build a Project
Signup Login & Registration Programs
Job Interview Preparation Questions
& Much More

Guide to Python Programming Language

Guide to Python Programming Language

Guide to Python Programming Language

The course will lead you from beginning level to advance in Python Programming Language. You do not need any prior knowledge on Python or any programming language or even programming to join the course and become an expert on the topic.

The course is begin continuously developing by adding lectures regularly.

Please see the Promo and free sample video to get to know more.

Hope you will enjoy it.

Basic knowledge
An Enthusiast Mind
A Computer
Basic Knowledge To Use Computer
Internet Connection
What will you learn
Will Be Expert On Python Programming Language
Build Application On Python Programming Language

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Hello and welcome to brand new series of wiredwiki. In this series i will teach you guys all you need to know about python. This series is designed for beginners but that doesn't means that i will not talk about the advanced stuff as well.

As you may all know by now that my approach of teaching is very simple and straightforward.In this series i will be talking about the all the things you need to know to jump start you python programming skills. This series is designed for noobs who are totally new to programming, so if you don't know any thing about

programming than this is the way to go guys Here is the links to all the videos that i will upload in this whole series.

In this video i will talk about all the basic introduction you need to know about python, which python version to choose, how to install python, how to get around with the interface, how to code your first program. Than we will talk about operators, expressions, numbers, strings, boo leans, lists, dictionaries, tuples and than inputs in python. With

Lots of exercises and more fun stuff, let's get started.

Download free Exercise files.


Who is the target audience?

First time Python programmers
Students and Teachers
IT pros who want to learn to code
Aspiring data scientists who want to add Python to their tool arsenal
Basic knowledge
Students should be comfortable working in the PC or Mac operating system
What will you learn
know basic programming concept and skill
build 6 text-based application using python
be able to learn other programming languages
be able to build sophisticated system using python in the future

To know more: