Contents
- Introduction
- List Comprehension in its simplest form
- Apply a condition to List Comprehension
- List Comprehension with Iterators
- List Comprehension with List as input
- List Comprehension with Range as input
- List Comprehension with Set as input
- List Comprehension with Dictionary keys as input
- List Comprehension with Dictionary values as input
- List Comprehension with String as input
- Element Transformation
- Summary
List Comprehension
List Comprehension is used to create a new list from a given iterator. List Comprehension offers a shorter syntax to create lists from an iterator, unlike looping statements.
We can also include conditions or transformations while creating lists using List Comprehension. We can also create multidimensional lists using List Comprehension.
In this tutorial, we will go through different scenarios to understand how List Comprehension can be written or used in Python, with example programs.
List Comprehension in its simplest form
In this example, we use List Comprehension to copy elements of a list to another list. This may not be a great example to copy an existing list to another list, but it gives a great introduction to List Comprehension in its simplest form.
Python Program
input = ['apple', 'banana', 'cherry']
output = [x for x in input]
print(output)
Run Code Online Output
['apple', 'banana', 'cherry']
Apply a condition to List Comprehension
Now, let us apply a condition whether to add the element from the given list to the new list.
We take a list of strings, and shall give the condition that the element must contain the character 'a'
to make it to the output list.
Python Program
input = ['apple', 'banana', 'cherry']
output = [x for x in input if 'a' in x]
print(output)
Run Code Online Output
['apple', 'banana']
As an another example, we take a list of numbers, and create a new list with only even numbers from the original list. So, our condition would be to check if the element from the original list is an even number.
Python Program
input = [1, 4, 5, 6, 8, 9, 10, 11, 21, 25, 28]
output = [x for x in input if x % 2 == 0]
print(output)
Run Code Online Output
[4, 6, 8, 10, 28]
List Comprehension with Iterators
We already know that we can create a new list from an iterator object using List Comprehension.
In the following example, we create a list from different iterator objects like: list, range, set, dictionary, string, etc.
1. List Comprehension with List as input
Python Program
input = ['apple', 'banana', 'cherry']
output = [x for x in input]
print(output)
Run Code Online Output
['apple', 'banana', 'cherry']
2. List Comprehension with Range as input
Python Program
input = range(4)
output = [x for x in input]
print(output)
Run Code Online Output
[0, 1, 2, 3]
3. List Comprehension with Set as input
Python Program
input = set(['apple', 'banana', 'cherry'])
output = [x for x in input]
print(output)
Run Code Online Output
['cherry', 'apple', 'banana']
4. List Comprehension with Dictionary keys as input
Python Program
input = {'apple': 28, 'banana': 55, 'cherry': 64}
output = [x for x in input]
print(output)
Run Code Online Output
['apple', 'banana', 'cherry']
5. List Comprehension with Dictionary values as input
Python Program
input = {'apple': 28, 'banana': 55, 'cherry': 64}
output = [x for x in input]
print(output)
Run Code Online Output
['apple', 'banana', 'cherry']
6. List Comprehension with String as input
Python Program
input = 'python'
output = [x for x in input]
print(output)
Run Code Online Output
['p', 'y', 't', 'h', 'o', 'n']
Element Transformation
We can also apply transformations to the elements in the List Comprehension.
In the following example, we take a list of strings as input, and create a new list with all the elements from the input list transformed to uppercase.
Python Program
input = ['apple', 'banana', 'cherry']
output = [x.upper() for x in input]
print(output)
Run Code Online Output
['APPLE', 'BANANA', 'CHERRY']
Now, let us create a new list with the length of strings in input list as elements.
Python Program
input = ['apple', 'banana', 'cherry']
output = [len(x) for x in input]
print(output)
Run Code Online Output
[5, 6, 6]
Summary
In this tutorial of Python Examples, we got introduced to List Comprehension in Python. There are many other scenarios where List Comprehension is used, and how it is used. We shall go through all of them in our following tutorials.
- List Comprehension with Two Lists
- List Comprehension containing If Condition
- List Comprehension with Multiple Conditions
- List Comprehension with List of Lists