List Comprehension
In this topic, I am going to explain about list comprehension in python with few examples. You might be wondered about the syntax that used in python programming. The other programming languages are not having this flexibility.
List Comprehensions provide an easiest way for creating new lists.
For example, i want to create a list of ’10’ elements range from ‘0’ to ’10’. One thing i can do here is, first i will create an empty list and will append the elements one after another using for loop.
The output of the program is something like this:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] |
Is there any other alternative way to do the same task?
The answer is ‘yes’, with list comprehension it is very easy to create a list. It almost takes only a line of code. Type the following (first line)snippet in IDLE and run it.
We can also store the result in to another variable and print the result.
The output of this program is similar to the one which we got in the earlier program.
In the above statement, we used the list comprehension. First let us see the syntax of this.
Syntax:
variable = [expression for-clause if-clause]
The list comprehension starts with a ‘[‘ and ‘]’ , meaning that the result is going to be a list. This syntax has three parts, there is no specific meaning for any color shown in the syntax. It is only for representation purpose.
expression : What you want to do?
for-clause : from which you want to iterate and how many times?
if-clause : do you want all the elements or you want to filter any elements?
Now, divide the above line of code in to three parts. I have not added the if-clause.
Expression : adding element ‘i’ to the list
for -clause : range of elements from 0 to 10
Now, in the following example i am adding if-clause to add the even numbers in to the list from the range 0 to 10.
The output of the above program is something like this:
[0, 2, 4, 6, 8, 10] |
Here, i am giving some more examples on list comprehensions. I hope that you that you will get more clarity once you practice all the examples given below. Each example of the list comprehension will give a list as output and you can do some other useful operations after getting the resultant list. I have shown the program along with the program output.
Read a list of numbers from one list and computer the square of each element and store the result in to another list.
[1, 4, 9, 16, 25, 36, 49] |
Read a string and store each character in the string in to another list
[‘s’, ‘u’, ‘d’, ‘h’, ‘a’, ‘k’, ‘a’, ‘r’] |
Read a string and the letters ‘a’ in to another list.
[‘a’, ‘a’] |
Print the numbers which are divisible by 3 and 5 in the given range (0,21)
[0, 15] |
Read a string and print only the numbers i.e. digits in to a separate list.
[‘9’, ‘9’, ‘8’, ‘5’, ‘3’, ‘2’, ‘7’, ‘1’, ‘9’, ‘9’] |
Read a list of strings and capitalize each string and store in another list.
[‘Sudhakar’, ‘Binish’, ‘Rathnam’, ‘Jai’, ‘Sowmya’] |
Printing the list of string, which are ends with letter ‘s’ from another list.
[‘this’, ‘strings’, ‘ends’, ‘s’] |
Similarly you can use with starswith() method to print the string which starts with a specific character.
The following program is something different with the above programs. It prints even or odd for each element in the list and print the result in a separate list.
[‘even’, ‘even’, ‘odd’, ‘odd’, ‘odd’, ‘odd’, ‘even’, ‘even’, ‘odd’, ‘odd’] |
Transposing the elements of a two dimensional matrix
[[1, 3, 5, 7], [2, 4, 6, 8]] |
Using nested for loops
[50, 60, 70, 60, 70, 80, 70, 80, 90] |
You can practice more examples from the following link: