In this section, we will learn what List Comprehension is and how to use it in Python.
What is List Comprehension in Python?
Python list comprehension is a shorter method of creating a list of items based on the values of another iterable object.
Using List Comprehension also gives us the ability to run conditions and tests on top of the elements of the target list in order to filter what element can become part of the new list.
Python List Comprehension Syntax:
newlist = [expression for item in iterable if condition == True]
`newlist`: this is the final list that will be produced after the operation of the list comprehension is done.
`for item in iterable`: this is a simple for loop that will go through the items of the target iterable object (like another list). Here the `iterable` is the target iterable object. The `item` is the returned element from the iterable object per each iteration. We put `in` between the `item` and `iterable` in order to separate them. And of course the for loop starts with the keyword `for`.
` if condition == True`: After each iteration in the loop, the returned `item` will be passed to this optional if condition. Here we can run a condition on top of the item and if it passed, it will be passed to the `expression` part of the list comprehension! Remember, this if condition is optional and can be ignored.
For example, the condition could be as simple as (if item not in [“Jack”,”Omid”,”Elon”]) meaning as long as the returned item from the target iterable object is not one of these mentioned elements, then it can be passed to the `expression` to become part of the new list.
`expression`: expression can be as simple as a variable name like `item` for example! This expression represents the item that will be passed as part of the elements of the new list. We say it’s an expression because we are allowed to modify the item before it becomes part of the new list as well! For example, if the new elements are all of type number, and we need to multiply them by the number 10 before they are being added to the list, then we can put the expression right here as `item * 10`.
Example: using List Comprehension in python
list1 = [1,2,3,4,5,6,7,8,9,10] list2 = [item * 10 for item in list1] print(list2)
Output:
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
Here the for loop is `for item in list1` and as you can see, the iterable object is `list1`.
The expression is `item * 10` and that means multiply the final items (those that are going to be added to the new list) by 10.
Note that in this example, we didn’t use the `if condition` part because, as mentioned before, it is optional.
Example: using python list comprehension with if statement
list1 = ["John","Jack","Omid","Ellen","Elon","Ian","Jame","Richard"] list2 = [item for item in list1 if item not in ["Jack","Omid","Elon"]] print(list2)
Output:
['John', 'Ellen', 'Ian', 'Jame', 'Richard']
Example: Iterating Through a String Using List Comprehension
A string is an iterable object as well. That means we can use a string as the target iterable object and get its characters and put them as the elements of the new list!
fullName = "John Doe" list1 = [character.upper() for character in fullName if character !=" "] print(list1)
Output:
['J', 'O', 'H', 'N', 'D', 'O', 'E']