Python is a high-level, interpreted programming language, and is one of the most popular programming languages present in the software industry. One of the most important but often overlooked concepts in python is that of list comprehensions. Let us have a look at it in detail.
In python, list comprehensions are used when we need to create a new list based on the data present in a pre-existing iterable such as a string, list, tuple, etc.
Instead of a relatively longer for loop statement, list comprehensions provide us with a shorter one-line statement to perform this task.
This method also happens to be more time and space-efficient when compared to using loops for the same task.
List comprehensions return a new list while the original list remains unchanged.
The syntax for using list comprehensions is pretty straightforward and is as follows. As usual, we use square brackets [ ] to enclose a list.
This consists of a for loop iterating over an iterable with a condition and the resulting expression results in an element of the list.
<new_list> = [ <expression> for <item> in <iterable> if <condition> ]
We can also use list comprehension to create a new list from a pre-existing list without any condition. The syntax for the same is as follows:
<new_list> = [ <expression> for <item> in <iterable> ]
Here, we used list comprehension to create a new list based on an existing string.
The original string here is hello. The list, we created will have one character of the string hello per element.
Let us look at the corresponding python code and output for this example:
# Create a string
a = 'Hello'
# Apply list comprehension
new_list = [ch for ch in a]
# Print new list
print(new_list)
Output
['H', 'e', 'l', 'l', 'o']
Now let us say we want to have each character in uppercase instead of original, we use the string.upper() function here that returns the uppercase character for each character.
Let us look at the corresponding python code and output for this example:
# Create a string
a = 'Hello'
# Apply list comprehension
new_list = [ch.upper() for ch in a]
# Print new list
print(new_list)
Output
['H', 'E', 'L', 'L', 'O']
Now, let us say we do not need the character L in our new list. We use a condition to specify this requirement using character != ‘L’. Let us look at the corresponding python code and output for this example:
# Create a string
a = 'Hello'
# Apply list comprehension
new_list = [ch.upper() for ch in a if ch != 'l']
# Print new list
print(new_list)
Output
['H', 'E', 'O']
Let us look at how we can perform list comprehension using other types of iterables such as lists, tuples and the range function.
Here we use list comprehension to create a new list based on an existing list.
The original list has numbers 0,1,2 and 3. The list we create will have all numbers except 2.
We specify this using a condition item != 2. Let us look at the corresponding python code and output for this example:
# Create a list
a = [0, 1, 2, 3]
# Apply list comprehension
new_list = [item for item in a if item != 2]
# Print new list
print(new_list)
Output
[0, 1, 3]
Here, we use list comprehension to create a new list based on an existing tuple.
The original tuple has numbers 0,1,2 and 3. The list we create will have all numbers of the tuple.
Let us look at the corresponding python code and output for this example:
# Create a tuple
a = (0, 1, 2, 3)
# Apply list comprehension
new_list = [item for item in a]
# Print new list
print(new_list)
Output
[0, 1, 2, 3]
Here, we use list comprehension to create a new list based on the result obtained from the range() function.
If a parameter of 4 is passed to the range() function, it returns the numbers 0,1,2, and 3.
The second list we create will have all numbers except 2. We specify this using a condition item != 2.
Let us look at the corresponding python code and output for this example -
# Apply list comprehension with range
new_list = [item for item in range(4)]
# Print new list
print(new_list)
# Apply list comprehension with condition
new_list = [item for item in range(4) if item != 2]
# Print new list
print(new_list)
Output
[0, 1, 2, 3]
[0, 1, 3]
In this topic, we have learned the use and advantages of list comprehensions in a Python program, following some simple running examples of programs, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to info.javaexercise@gmail.com in case of any suggestions.