Sam  Son

Sam Son


Using List Comprehensions in Python like Master

Many more people are starting to learn Python, as it has become one of the most popular programming languages for almost anything, like web development, scientific computing, and certainly artificial intelligence.

No matter where you’re going with Python, you unavoidably have to learn Python’s data structures, variable and function declarations, conditional statements, control flows, and other basic concepts.

One important “Pythonic” feature that can be puzzling to many beginners is the list comprehension—a concise way to create lists.

Heard of it before, but don’t know what it is?

Great, this article will provide you with a head-start for mastering list comprehensions in your Python learning adventure.

For the purpose of easier organization, I’ve listed nine things that we should know about list comprehensions, including its syntax and various use cases.

1. Basic Syntax

The most basic list comprehension has the following syntax.

As mentioned previously, it serves as a concise way of doing certain things, such as creating lists. The expanded form is usually expressed as a for loop, in which each item of the iterable runs certain operations as specified in the expression.

# list comprehension
[expression for item in iterable]

# expanded form
for item in iterable:

2. Create a List

It’s not surprising at all that the most popular usage is to create a list concisely.

Suppose that we don’t know list comprehensions, we’ll probably do something like the below when it comes to the creation of a list. To do that, first, we’ll declare an empty list. Second, in the for loop, we append each item to the list.

>>> pets = ('bird', 'snake', 'dog', 'turtle', 'cat', 'hamster')
>>> uppercased_pets = []
>>> for pet in pets:
...     uppercased_pets.append(pet.upper())
>>> uppercased_pets

As mentioned in the basic syntax section, we can “compress” the for loop statement into one line — using the list comprehension with just one line of code, we can conveniently create a list by iterating the original list.

>>> pets = ('bird', 'snake', 'dog', 'turtle', 'cat', 'hamster')
>>> uppercased_pets = [pet.upper() for pet in pets]
>>> uppercased_pets

3. Conditional Statement for Filtering

Sometimes, when we use list comprehensions to create a list, we don’t want to include all of the items on the existing list.

In this case, we need a conditional statement to filter out the items in the existing list that don’t meet certain criteria. The corresponding list comprehension has the following syntax.

# list comprehension with a conditional statement
[expression for item in iterable if some_condition]
# expanded form
for item in iterable:
    if some_condition:

Here’s an example of this usage.

>>> primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
>>> squared_primes = [x*x for x in primes if x%10 == 3]
>>> squared_primes
[9, 169, 529]

If we have a more complicated evaluation of the condition, we can even use a function.

>>> def has_four_legs(pet):
...     return pet in ('pig', 'dog', 'turtle', 'hamster', 'cat')
>>> pets = ('bird', 'snake', 'dog', 'turtle', 'cat', 'hamster')
>>> four_legs_pets = [pet.capitalize() for pet in pets if has_four_legs(pet)]
>>> four_legs_pets
['Dog', 'Turtle', 'Cat', 'Hamster']

4. Conditional Assignment

Sometimes, we don’t want to filter out the items from the original list.

Instead, we want to evaluate the condition to determine which expression is used. The syntax and its usage are given below. The syntax is also explained below.

# basic syntax
[expression0 if some_condition else expression1 for item in iterable]
# syntax explained: compared to the list comprehension's basic syntax: [expression for item in iterable], we can thin about that (expression0 if some_condition else expression1) is a whole part that constitutes the expression in the general format
>>> max_value = 10
>>> numbers = (7, 9, 11, 4, 3, 2, 12)
>>> ceiling_numbers0 = [number if number <= max_value else max_value for number in numbers] 
>>> ceiling_numbers0
[7, 9, 10, 4, 3, 2, 10]
>>> ceiling_numbers1 = [(number if number <= max_value else max_value) for number in numbers]
>>> ceiling_numbers1
[7, 9, 10, 4, 3, 2, 10]

5. Replace map()

In some situations, you may have seen people use map() to create a list. Specifically, the map() function has the following syntax together with an example of its basic usage.

One thing to note is that the map() function returns an iterable object, and thus we can use the list() function to generate a list from this iterable.

# map() returns an iterator object
map(function, iterable)
>>> pets = ('bird', 'snake', 'dog', 'turtle', 'cat', 'hamster')
>>> uppercased_pets = list(map(str.upper, pets))
>>> uppercased_pets

As shown previously, we can replace the map() function with the list comprehension.

>>> pets = ('bird', 'snake', 'dog', 'turtle', 'cat', 'hamster')
>>> uppercased_pets = [pet.upper() for pet in pets]
>>> uppercased_pets

6. Nested List Comprehensions

Suppose that we have a tuple in the code snippet below, and we want to create a new list of items that are squares of all numbers in the tuple.

In this case, we can use the nested list comprehension, the syntax of which is also shown below.

# basic syntax of the nested list comprehensions
[expression for sublist in outer_list for item in sublist]
# expanded form
for sublist in outer_list:
    for item in sublist:
>>> nested_numbers = ((1, 4, 7, 8), (2, 3, 5))
>>> squares = [x*x for numbers in nested_numbers for x in numbers]
>>> squares
[1, 16, 49, 64, 4, 9, 25]

Although it’s technically possible to have multiple levels for the nested list comprehensions, for readability, it’s not recommended to have more than two levels.

7. Use Walrus Operator

One of the new features in Python 3.8 is the introduction of the walrus operator (:=), which is used in assignment expression.

Suppose that we want to draw ten times from a list of letters, and the list that we’re creating will only include vowels from these drawings. Here’s how we can do it using the walrus operator in the list comprehension.

Specifically, in the example below, we evaluate whether a random letter drawn from the letters is a vowel, and if it is, it’ll be assigned to the letter to which the list comprehension’s expression can have access.

>>> letters = list('this is to produce a list of letters')
>>> letters
['t', 'h', 'i', 's', ' ', 'i', 's', ' ', 't', 'o', ' ', 'p', 'r', 'o', 'd', 'u', 'c', 'e', ' ', 'a', ' ', 'l', 'i', 's', 't', ' ', 'o', 'f', ' ', 'l', 'e', 't', 't', 'e', 'r', 's']
>>> import random
>>> vowels = [letter.upper() for _ in range(0, 10) if (letter := random.choice(letters)) in list('aeoui')]
>>> vowels
['I', 'O', 'O', 'O', 'O']

8. Set Comprehension

Although the list comprehension is known to many people, we can also use comprehension when we create a set. The basic syntax and its usage are shown below.

One major difference is that we use curly braces instead of square brackets. Certainly, by design, the elements in a set won’t have duplicates as opposed to a list where duplicates are allowed.

Please note that we can also use a conditional statement in a set comprehension.

# syntax for set comprehension
{expression for item in iterable}
>>> numbers = (1, 34, 5, 8, 10, 12, 3, 90, 70, 70, 90)
>>> unique_even_numbers = {number for number in numbers if number%2 == 0}
>>> unique_even_numbers
{34, 70, 8, 10, 12, 90}

9. Dict Comprehension

We have list and set comprehensions, and you won’t be surprised to learn that Python also has dict comprehension. The basic syntax and its usage are shown in the following code snippet.

# syntax for dict comprehension
{key_expression : value_expression for item in iterable}
>>> words = ('python', 'is', 'a', 'big', 'snake')
>>> len_words = {word : len(word) for word in words}
>>> len_words
{'python': 6, 'is': 2, 'a': 1, 'big': 3, 'snake': 5}
>>> len_words_p = {word : len(word) for word in words if word.startswith('p')}
>>> len_words_p
{'python': 6}


This article reviews the basic syntax of list comprehensions and their usage in various scenarios.

Beyond the list comprehension, we also talked about the set and dict comprehensions. These comprehensions allow us to create these basic collection data types very conveniently in Python with better readability.

Thank you for reading!

#python #Data Science #programming #development

What is GEEK

Buddha Community

Using List Comprehensions in Python like Master
Ray  Patel

Ray Patel


top 30 Python Tips and Tricks for Beginners

Welcome to my Blog , In this article, you are going to learn the top 10 python tips and tricks.

1) swap two numbers.

2) Reversing a string in Python.

3) Create a single string from all the elements in list.

4) Chaining Of Comparison Operators.

5) Print The File Path Of Imported Modules.

6) Return Multiple Values From Functions.

7) Find The Most Frequent Value In A List.

8) Check The Memory Usage Of An Object.

#python #python hacks tricks #python learning tips #python programming tricks #python tips #python tips and tricks #python tips and tricks advanced #python tips and tricks for beginners #python tips tricks and techniques #python tutorial #tips and tricks in python #tips to learn python #top 30 python tips and tricks for beginners

Ray  Patel

Ray Patel


Lambda, Map, Filter functions in python

Welcome to my Blog, In this article, we will learn python lambda function, Map function, and filter function.

Lambda function in python: Lambda is a one line anonymous function and lambda takes any number of arguments but can only have one expression and python lambda syntax is

Syntax: x = lambda arguments : expression

Now i will show you some python lambda function examples:

#python #anonymous function python #filter function in python #lambda #lambda python 3 #map python #python filter #python filter lambda #python lambda #python lambda examples #python map

August  Larson

August Larson


How to Convert Loops to List Comprehension in Python

Do the same but faster

List comprehension is used for creating lists based on iterables. It can also be described as representing for and if loops with a simpler and more appealing syntax. List comprehensions are relatively faster than for loops.

The syntax of a list comprehension is actually easy to understand. However, when it comes to complex and nested operations, it might get a little tricky to figure out how to structure a list comprehension.

In such cases, writing the loop version first makes it easier to write the code for the list comprehension. We will go over several examples that demonstrate how to convert a loop-wise syntax to a list comprehension.

Basic structure of list comprehension (image by author)

Let’s start with a simple example. We have a list of 5 integers and want to create a list that contains the squares of each item. Following is the for loop that performs this operation.

lst_a = [1, 2, 3, 4, 5]

lst_b = []
for i in lst_a:
[1, 4, 9, 16, 25]

#python #programming #how to convert loops to list comprehension in python #convert loops #list comprehension #how to convert loops to list comprehension

Osiki  Douglas

Osiki Douglas


List Comprehension

List comprehension is nothing but a shorter and crisper version of the code and also memory efficient. By using this we can either create a new list or perform some operation in an existing list.

The normal code for creating a list of 0–9 will be like

for i in range (10):

By using list comprehension

x=[i for i in range(10)]


As you can see the normal code is long but the code that we did using list comprehension does the job just in one line so list comprehension is preferred over the traditional method.

#list-comprehension #lists #python #python-list-comprehension

Python Tips and Tricks for Competitive Programming

Python Programming language makes everything easier and straightforward. Effective use of its built-in libraries can save a lot of time and help with faster submissions while doing Competitive Programming. Below are few such useful tricks that every Pythonist should have at their fingertips:

  • **Converting a number into a List of digits using map() Function: **

Below is the implementation to convert a given number into a list of digits:

#competitive programming #python programs #python-itertools #python-library #python-list #python-list-of-lists #python-map