Table of Content
- What is List Comprehensions in Python
- Syntax of List Comprehensions in Python
- Example of List Comprehension
- How to Use Conditionals in List Comprehension
- How to Use Nested Loops in List Comprehension
- Wrap Off
List comprehensions are a syntax that's sometimes preferred over loops, as it's more readable when the operation can be written on a single line.
Supposing you have a list of fruits, you want a new list, containing only the fruits with the letter “a” in the name.
Without list comprehension, you will have to write a for
statement with a conditional test inside. With list comprehension you can do all that with only one line of code.
What is List Comprehensions in Python
List comprehensions are a way to create lists in a very concise way.
It offers a shorter syntax when you want to create a new list based on the values of an existing list.
List comprehension is generally more compact and faster than normal functions and loops for creating list.
However, we should avoid writing very long list comprehensions in one line to ensure that code is user-friendly.
Remember, every list comprehension can be rewritten in for loop, but every for loop can't be rewritten in the form of list comprehension.
Syntax of List Comprehensions in Python
Here is the syntax of list comprehension in Python:
newlist = [_expression_ for _item_ in _iterable_ if _condition_ == True]
- Condition: The condition is like a filter that only accepts the items that valuate to
True
. The condition is optional and can be omitted. - Iterable: The iterable can be any iterable object, like a list, tuple, set etc.
- Expression: The expression is the current item in the iteration, but it is also the outcome, which you can manipulate before it ends up like a list item in the new list. You can set the outcome to whatever you like such as power of 2 for integers or uppercase for strings. The expression can also contain conditions, not like a filter, but as a way to manipulate the outcome
Example of List Comprehension
Suppose you have a list:
numbers = [1, 2, 3, 4, 5]
Using For loops
You can create a new list using regular for loop by doing the following:
numbers_power_4 = []
for n in numbers:
numbers_power_4.append(n**4)
Using lambda function with map()
numbers_power_4 = list(map(lambda n : n**4, numbers))
Using List Comprehension
However, you can also create a new list using a list comprehension, composed by the numbers
list elements, power 4:
numbers_power_4 = [n**4 for n in numbers]
How to Use Conditionals in List Comprehension
List comprehensions can utilize conditional statement to modify existing list (or other tuples).
Example of List Comprehension in Python Using “If” Conditionals
We will create list that uses mathematical operators, integers, and range()
Example of List Comprehension in Python Using Nested “If” Conditionals
In the example above, the list comprehension checks:
- Can y be divided by 2?
- Can y be divided by 5?
If y satisfies both conditions, y is appended to num_list.
Example of List Comprehension in Python Using “If … Else” Conditionals
obj = ["Even" if i%2==0 else "Odd" for i in range(10)]
print(obj)
How to Use Nested Loops in List Comprehension
Example of Transpose of Matrix using Nested Loops in List Comprehension
Supposing we have a 2D array, use can two forloops to find transpose of the matrix.
However, we can also perform nested iteration inside a list comprehension.
Wrap Off
Without list comprehension you will have to write a for
statement with a conditional test inside. With list comprehension you can do all that with only one line of code
We hope this covers everything you need to know about list comprehensions in Python.
If you learned from this tutorial, or it helped you in any way, please consider sharing and subscribing to our newsletter.