How to use Python List Comprehension – with example

  • Reading time:15 mins read

What is the Python List Comprehension method?

Python List comprehension is an easy and precise way of creating lists from pre-existing iterable objects such as lists, tuples etc.

 

 

How to use Python List Comprehension?

For instance we can create a list with some numbers from a range. Usually one starts off by creating a blank list followed by a for x in range(1, 11). And then we will do newlist.append(x).

This is going to create a list and append the value of x every time we run through.

 

Shorten our code

However, using python list comprehension we can write fewer lines of code but get the same output. So what we need to do is again start with the name of the list. This is the start of the list comprehension. In this case, we will name the list, ‘listcomp’ is equal to and we now open the square brackets.

 

Python List Comprehension is a useful tool while coding

 

Now within the brackets, we need to write x for x in range(1,11). This means that for every value within the range of 1 to 11 represented by x create a list of x.

Now if we print the list ‘listcomp’ we will definitely obtain a list the same as the one we created using the for loop.

That’s the simplest, simplest comprehension that is much more elegant compared to the three lines of code we had previously used.

 

Conditional list appending

Suppose we only wanted to create a list with numbers only greater than three. In such a case we would say if x is greater than three newlist.append.

 

 

So now down here, we can see the first list here is now 4 to 10 because we’re appending x only if it is greater than three. 

Conditional list appending by using Python list comprehension

We can also do this as well using list comprehension. This will only require us to add another line of code at the end of the preexisting one that reads if x is greater than three.

 

 

Example 3

In this example, we are going to use a list of animals = [‘elephants’, ‘lions’, ‘lions’, ‘giraffe’, ‘antelope’]. 

Let’s say we only want animals that have the letter ‘a’ in them, we can either use the for loop to iterate through the entire list or filter only elements with the letter a. As shown here.

 

 

Using list comprehension on the other hand we only need to state this in one line. 

 

Learn Python List Comprehension online

 

Nice and neatly will say newlist is equal to animal for animal in animals if ‘a’ in animal. So this does look a little bit confusing, but what the code is doing, is essentially what we did before and every time it references the word animal and checks if it contains that letter ‘a’ and then proceeds to append it to the newlist.

 

Summary

So this is how the Python list comprehension works in practice. If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.