Python List Comprehension

Python-List-Comprehension-ipcisco-1

Python List Comprehension is one of the most used python list functions. The main aim of this syntax is creating a new list from th existing list according to a condition. For example, we can check a list of strings and if a certain characetr exist in any of this string, we can create a new list from this list.

 

Let’s do an example to show how can we use Python List Comprehension. BElow, in our python string example, we will create list2 from list1 nad then we will print list2. Here, we will check if “a” exist in any of the members of existing list, list1.

 

list1 = ["aab", "aaa", "bcd", "cdae", "bbfd"]
list2 = [x for x in list1 if "a" in x]
print(list2)

 

The output of this List Comprehension example will be like below:

 

['aab', 'aaa', 'cdae']

 

Python-List-Comprehension-ipcisco

As you can see above, onlty the strings that include “a”, is attend as a member of the new list. Here, the expression that we write in list2 is important. Here, we are saying that take any member in list1 if it has “a” inside and put it as a member to new list, list2.

 

You can also watch the video of this lesson!

 

We can do this Python List Comprehansion with also numbers. Let’s check the below example for this code.

 

list1 = ["152", "4", "98", "169", "16"]
list2 = [x for x in list1 if "9" in x]
print(list2)

 

The loutput of this Python List Comprehension example will be:

 

['98', '169']

 

With Python List Comprehension, we can also search for a certain word in any string or numeric lists. For example in the below string list thatb has 5 member, we will search for “dogs” and we will add the strings that includes “dogs” to the new list.

 

list1 = ["cats are cute", "dogs are friendly", "cats and dogs are friends", "I like dogs", "I like cats"]
list2 = [x for x in list1 if "dogs" in x]
print(list2)

 

As you can see below, the otput of this python code will include only the strings that include “dogs” in the lists.

 

['dogs are friendly', 'cats and dogs are friends', 'I like dogs']

 

We can also use Python List Comprehension syntax without an existing list. Below, we will create a list with range function  and a if condition. Here, willl check the numbers from 0 to 9 and if these numbers are smaller than 8 and higher than 2, we will create a list with these numbers.

 

list = [x for x in range(10) if (x < 8) & (x > 2) ]
print(list)

 

[3, 4, 5, 6, 7]

 

 

Python-List-Comprehension-range-method

Let’s show another example that will list the squares of the given number range and assign it to a new list.

 

list = [x for x in range(10)]
print(list)

squarelist = [x * x for x in range(10)]
print(squarelist)

 

The  output of this python code will be:

 

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

 

python-list-range-method-square

Lastly, let’s do another example, that will lsit the letters of a string in a list. Below, we will create a new list with the letters of Aragorn.

 

letters = [x for x in "Aragorn"]
print(letters)

 

The output of this Python List Comprehension example will be like below:

 

['A', 'r', 'a', 'g', 'o', 'r', 'n']

 

Python-List-letter-sort-ipcisco-1

In this lesson, we have seen how to do List Comprehension with different python list examples. You can do differetn eaxmple to learn comprehension of python lists. With these new example, you will learn the function of this systax better. And day by day, you can create more complex python code related with Python List Comprehension.

 

Back to: Python Programming Course > Python Lists

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact

  • info@ipcisco.com
IPCisco is the Winner! “Best Certification Study Journey of 2019!”

Cisco-ITBlogAwards-2019-Winner-IPCisco-k