Lists are important tools for describing any data in a concise format. In other words, they improve the understanding and manipulation process of data. Python programming language provides us with the functionality of listing items. The programming codes get shorter and easily decipherable, hence reducing the chances of the error on one hand and increasing the execution pace of the program.
If you are well familiar with the list functions in Python, then the list comprehensions can be handy for coding as they help in making new syntax for any problem in Python. Here, in this list comprehension python tutorial, we will explore List comprehensions and understand the importance of using them in our Python code. So let us look at what we will be learning via this Index.
Table of Contents
List comprehensions are tools that allow us to transform one list into another list. This transformation changes the way code will be interpreted and makes it easier. We can include elements conditionally in the list and these elements can be transformed as per the need. The multiline statements can be avoided with the usage of the list comprehensions.
A list comprehension Python is enclosed in the square brackets. They always include the keywords, ‘for
’ and ‘in
’. They are important tools to learn if you are looking for faster programming. Next, we will see how we can do that and how the List comprehensions provide the time advantage.
If you want enrich your career and become a professional in python, then visit Mindmajix - a global online training platform : "python certification" This course will help you to achieve excellence in this domain
The Time Advantage
List comprehensions save a lot of time by writing the code in a different manner like multiple statements into single lines. The only thing you have to look for is where you can use the list comprehension.
Here, let’s look at few examples.
Now this code using ‘for’ loop is written in multiple statements as you can see.
new_things = []
for ITEM in old_things:
if condition_based_on(ITEM):
new_things.append("something with " + ITEM)
The above code can be written in the following way using the list comprehensions.
new_things = ["something with " + ITEM for ITEM in old_things if condition_based_on(ITEM)]
Both codes are describing the same thing.
This is why the list comprehensions are very handy if you understand them. Here is another example:
mylist = ['ABC', 'fOO', 'bar']
tmp = []
for x in mylist :
tmp.append(x.lower())
mylist = tmp
In this example, we have a list of strings. We require the strings which are in lowercase. However, we can see according to the python standards that it’s a lot of code for a really simple task. Now, we will use list comprehensions to write this code in an easier manner.
mylist = [x.lower() for x in mylist]
It saves a lot of time as we can witness the above code. So, is it convenient to use List comprehensions in Python? This is because list comprehensions in python are optimized for the python interpreter to spot the predictable pattern while looping, henceforth making the code’s execution faster.
[Related Page: Python Lists with Examples]
Generator expressions are similar like List comprehensions. However, in contrast to the list comprehensions, they focus on one by one execution of the items. In other words, as we know, list comprehension takes up all the required items and packs them up in the list whereas generator expression focuses upon generating one item at a time rather than stacking them into one. Instead of brackets, parentheses are used in the Generator Expressions.
Since only one item is being processed by the Generator expressions there is less usage of the memory. Generator expressions are useful when one has to deal with items that require a lot of work and calculation, whereas, in case if one needs all the values, then he/she should proceed with list compressions. Here is an example:
>>> my_list = [1, 3, 5, 9, 2, 6]
>>> filtered_list = [item for item in my_list if item > 3]
>>> print filtered_list
[5, 9, 6]
>>> len(filtered_list)
3
>>> # compare to generator expression
...
>>> filtered_gen = (item for item in my_list if item > 3)
>>> print filtered_gen # notice it's a generator object
>>> len(filtered_gen) # So technically, it has no length
Traceback (most recent call last):
File "", line 1, in
TypeError: object of type 'generator' has no len()
>>> # We extract each item out individually. We'll do it manually first.
...
>>> filtered_gen.__next__( )
5
>>> filtered_gen.__next__( )
9
>>> filtered_gen.__next__( )
6
>>> filtered_gen.__next__( ) # Should be all out of items and give an error
Traceback (most recent call last):
File "", line 1, in
StopIteration
>>> # Yup, the generator is spent. No values for you!
...
>>> # Let's prove it gives the same results as our list comprehension
...
>>> filtered_gen = (item for item in my_list if item > 3)
>>> gen_to_list = list(filtered_gen)
>>> print gen_to_list
[5, 9, 6]
>>> filtered_list == gen_to_list
True
>>>
In the code given above, we can see that all items are being handled one at a time.
You might have heard about iterators. If not, here is their description:
Iterators are objects which we can iterate or loop. Strings, lists, and dictionaries are few objects that we can iterate. Generators are helpful in marking any function as iterators.
Now, for understanding the use of Generators, we must in turn investigate iterators. Iterators perform what programmers remark “Lazy Evolution”.
Iterators compute value of any item only when asked. In other words, iterators don’t compute the value of each item soon after initialization. Iterators are useful when there is large amount of data required to be computed. Hence, we can use the data as per requirement.
Now that we are familiar with iterators, we will understand the importance of Generators.
Generators are special kind of functions. They use “yield
” statement to return data. Since iterators are a very important aspect of coding in Python, they are bound to be error free. Generators facilitate this as they are helpful in tracking each element due to their step by step usage. Hence, the generators must be efficiently used in the program.
Often, there are a lot of possible codes whose output may be infinite possible values. Since we very well know that we can’t store infinite values due to hardware and memory limitations, generators are quite useful in this case as they handle one item at a time.
[Related Page: Introduction to Python Programming]
As we already know that List comprehensions provide easier, more convenient and time saving way of coding in Python, we will now see how much useful they are in contrast to ‘for
’ loops.
We will understand it with the help of an example:
Here we will separate the letters of the word ‘earth
’ using ‘for
’ loop.
h_letters = []
for letter in 'earth':
h_letters.append(letter)
print(h_letters)
The output of this program will be:
['e', 'a', 'r', 't', 'h']
You can see that it is somewhat longer program for such an easy task. List comprehensions make this process easier.
Now, we will write the above program using list comprehensions:
h_letters = [ letter for letter in 'earth' ]
print( h_letters)
Again, the output will be:
['e', 'a', 'r', 't', 'h']
The variable h_letters in the above code is assigned with a new list. The list contains the item to be iterated i.e. string ‘earth
’.
It is clear from this program that the list comprehension provides a much easier way to write a code wherever applicable.
The major feature of the list comprehension is its ability to handle strings of tuple and handling it as a list. Whereas in the ‘for’ loops, Python assigns a name to each element of the iterable item and then executes as it names them.
Please note that not all loops can be written using list comprehension.
Frequency Asked Python Interview Questions
Apart from list comprehensions, Python also provides other built-in functions to handle the code in lesser lines. One such function is Lambda function.
Now, we will take the same code with what we used for List comprehension vs. ‘for
’ loop.
Now the program for list comprehension:
h_letters = [ letter for letter in 'earth' ]
print( h_letters)
And the output:
['e', 'a', 'r', 't', 'h']
Now, with the Lambda function:
letters = list(map(lambda x: x, 'earth'))
print(letters)
Again the output is:
['e', 'a', 'r', 't', 'h']
But, still, List comprehension is better than the lambda function. With lambda function, you have to use the ‘map
’ function. Lambda function is used for simple expression. In that case, ‘map’
usage results in function call for each element, whereas the list comprehension compiles it into a single loop that builds the list.
Even in cases of existing functions, list comprehension seems more usable as it keeps the code more readable than lambda function. So, most programmers prefer list comprehension but usage of the lambda function still depends upon personal choice.
For multiple iterations, one can use nested loops. The loops then can be flattened with the use of the List comprehension. This reduces the mishandling of the number of loops, especially if you are not that much familiar with Python.
For understanding nested loops, first we take an already existing ‘for
’ loop in consideration and then use it for list comprehension with multiple conditions. Then, using this code, we will get a new list which iterates 2 lists and then finally use it for mathematical operations, hence, nesting both lists. Here is the nested code block of ‘for
’ loop:
my_list = []
for x in [10, 20, 30]:
for y in [1, 3, 2]:
my_list.append(x * y)
print(my_list)
In this code, we are multiplying items in the first list by the item in the second list, for each iteration.
The output of the following code will be:
[10, 30, 20, 20, 60, 40, 30, 90, 60]
Now, we will transform this into list comprehension. For that, we will condense each line of code into the single line. We will first assign each list to a variable using outer and inner parts of the ‘for
’ loop and then execute the program. Then a print( ) statement will be used to receive the result.
Here is the nested loop for the code:
my_list = [x * y for x in [10, 20, 30] for y in [1, 3, 2]]
print(my_list)
In this code the first list [10, 20, 30] is being assigned to the variable ‘x’ whereas the second list [1, 3, 2] is being assigned to the variable ‘y’ using ‘for’
loop. Then x * y
determines the multiplication of both lists.
List comprehension flattens the both nested ‘for’ loops in the single line while creating the exact list which is to be assigned to the ‘my_list’
variable.
This clearly determines the usefulness of the list comprehension in which even multiple loops are flattened in the single line. However, the thing that should be kept in mind is that, in case, if we have large number of loops to be nested and readability of the code is important, then breaking up the code in loops will be a better choice.
List comprehension is compatible with using more conditional statements for modifying sequential data types or existing lists, while creating new lists. This can help in getting a more conditional and specific result when one has many lists to handle and output is required to be refined.
Here is an example of ‘if
’ statement used in list comprehension:
man_tuple = ('Venom', 'Ironman', 'Batman', 'Spiderman')
man_list = [man for man in man_tuple if man != 'Venom']
print(man_list)
Here, in this code, the list comprehension uses tuple ‘man_tuple
’ as basis for the new list called ‘man list
’ . Now, apart from the ‘for
’ and ‘in’ keywords, the ‘if
’ statement is being used to add those items to the output, which is not equivalent to the string ‘Venom’, hence the ‘if
’ statement takes those items from the tuple that do not match ‘Venom
’.
Now, in the output, we will see that ‘man’ list contains all the items as ‘man tuple
’ except the ‘Venom
’, which has been omitted.
['Ironman', 'Batman', 'Spiderman']
Now, we will see a more complex example that consists of mathematical operators, range ( ) sequence, and the integer type.
number_list = [x ** 3 for x in range(10) if x % 2 == 0]
print(number_list)
In this code, the list ‘number_list’, will contain cubed value of each item which is present in the range of 0-9, if the item’s value in this case is divisible by 2. Here is the output of the code:
[0, 8, 64, 216, 512]
The list comprehension is functioning more by limiting the number of items that will form the new list from the existing sequence.
if you are interested learn python and build a career in it ? Then checkout our python training near your cities
Python Training Chennai , Python Training New York , Python Training in Bangalore , Python Training in Dallas
These courses are incorporated with Live instructor-led training, Industry Use cases, and hands-on live projects. This training program will make you an expert in Microsoft Azure and help you to achieve your dream job.
Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:
Name | Dates | |
---|---|---|
Python Training | Dec 24 to Jan 08 | View Details |
Python Training | Dec 28 to Jan 12 | View Details |
Python Training | Dec 31 to Jan 15 | View Details |
Python Training | Jan 04 to Jan 19 | View Details |
Anjaneyulu Naini is working as a Content contributor for Mindmajix. He has a great understanding of today’s technology and statistical analysis environment, which includes key aspects such as analysis of variance and software,. He is well aware of various technologies such as Python, Artificial Intelligence, Oracle, Business Intelligence, Altrex, etc. Connect with him on LinkedIn and Twitter.