Python List Comprehension
Now, we are going to learn about a very interesting and useful concept, which is list comprehension. Basically, with list comprehension, we can create a list, using an existing list. The syntax is very easy, and there are many other benefits of making use of list comprehension we require fewer lines of code, and it is more time and space efficient when compared to loops. So, we are now going to study the concept of list comprehension.
Let’s say that we have a list of all the numbers, from 1 to 30. but here, we want to create another list, which contains only the even numbers from the previous list. This can be easily done with list comprehension (you can do it without list comprehension too, but this is our subject of interest as well). Let’s see how we can do that.
As you can see, we have a list, assigned to variable numbers, which contains numbers from 1 to 30. After that, we are doing list comprehension. Here, we simply get the list, which contains the even numbers, from 1 to 30. Now, Let’s understand what we have done in the list comprehension.
Python List Comprehension
Here is the syntax of list comprehension, so that you can get a clearer picture of what is list comprehension, how we do it, and how is it useful.
Newlist =[expression for item in oldlist if condition]
As you can see, within the square brackets, we have some expression(element), for some item in the oldlist, and then we have some conditions. Basically, each element in this list is going to be a result of some operation on each member of some another sequence, or iterable. Also, the elements can be those elements that satisfy certain condition.
For example, now Let’s have a look at such an example, where we are trying to have square of numbers from 1 to 10. Let’s create it with the help of list comprehension, and while we go through this example, the above syntax should make more sense.
As you can see in the above program, we have created a list of squares of numbers from 1 to 10. For this, we simply did list comprehension. So, you can use these things as and when required. You can even do this without list comprehension, but there are several benefits of list comprehension, due to which, we should be using list comprehension. You can even explore this, and try to implement it in several examples so that you can understand the concept of list comprehension.
So, this was much about the concept of lists. We have explored and implemented a lot of things, and we are going to make use of lists, as and when required in our python programs. There are many situations when you might want to make use of the lists in our programs. Like when we need to store multiple data, or since a list can store data of multiple datatypes, we can store all the information about something similar, into some list, and also make list of lists, or when we need to implement stack or queue, we can make use of lists in that case too. So, we would make use of lists, as and when required in our python programs.
Here is a list of some programs related to lists. You can try implementing them, and implement some more examples like this, so as to get familiar with the concept, and so that later you would be comfortable to use the concept.
- Write a Python Program to calculate the sum of elements(integers) present in the list below.
list1 = [1, 34, 54, 23, 6, 5, 34]. - Write a Python program to find the largest number from above list.
- Write a python program, in which you are given a list of int numbers, and you have to create another list in which you have to put only prime numbers from the given list. Here is the list you can refer –
list1 = [12, 17, 2, 34, 13, 11, 33, 45,67, 23].