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.
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.
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
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to use the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
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.
