Home Picture

Python Baisc: List Comprehension

13 Aug 2020 |

Categories: Python

List Comprehension

Table of contents:

List Comprehension

List comprehensions provide a concise way to create lists. It is frequently used to make new lists where every element is the result of some operations applied to anthor iterable object.


Simple List Comprehension

Create a list and square the number from 0 to 9:

For Loop Method:

import numpy as np
Alist=list()
for i in range(10):
    Alist.append(np.square(i))
print(Alist)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

List Comprehension Method:

import numpy as np
Alist = [np.square(i) for i in range(10)]
print(Alist)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]


Adding Condition

Square the number from 0 to 9, and store the odd number to a list:

For Loop Method:

import numpy as np
odd=list()
for i in range(10):
    num = np.square(i)
    if num % 2 !=0:
        odd.append(num)
print(odd)
[1, 9, 25, 49, 81]

List Comprehension Method:

import numpy as np
odd = [np.square(i) for i in range(10) if np.square(i)%2 !=0]
print(odd)
[1, 9, 25, 49, 81]


Adding if... else... Statement

Square the number from 0 to 9 and plus 1 for each of number. Then, create a list to store a list of strings.When the number is an odd number, store ‘odd’. Otherwise, store ‘even’.

For Loop Method:

import numpy as np
stringlist=list()
for i in range(10):
    num = np.square(i) + 1
    if num % 2 !=0:
        stringlist.append('odd')
    else:
        stringlist.append('even')
print(stringlist)
['odd', 'even', 'odd', 'even', 'odd', 'even', 'odd', 'even', 'odd', 'even']

List Comprehension Method:

The position of if statement has been moved to front and followed by the else statement

import numpy as np
stringlist = ['odd' if (np.square(i)+1)%2 !=0 else 'even' for i in range(10)]
print(stringlist)
['odd', 'even', 'odd', 'even', 'odd', 'even', 'odd', 'even', 'odd', 'even']


Nested ifs in list comprehension

1. Two ifs

For Loop Method:

import numpy as np
str5list=list()
for i in range(10):
    num = np.square(i) + 1
    if (num % 2 != 0):
        if (num % 5 == 0):
            str5list.append(num)
print(str5list)
[5, 65]

List Comprehension Method:

import numpy as np
str5list = [(np.square(i)+1) for i in range(10) if ((np.square(i)+1)%2 !=0) if ((np.square(i)+1)%5 ==0)]
print(str5list)
[5, 65]

2. Ifs and else with three outputs

For Loop Method:

import numpy as np
str5list=list()
for i in range(10):
    num = np.square(i) + 1
    if (num % 2 != 0):
        str5list.append('odd')
    else:
        if (num % 5 == 0):
            str5list.append('odd and True')
        else:
            str5list.append('false')
print(str5list)
['odd',
 'false',
 'odd',
 'odd and True',
 'odd',
 'false',
 'odd',
 'odd and True',
 'odd',
 'false']

List Comprehension Method:

import numpy as np
str5list = ['odd'  if ((np.square(i)+1)%2 !=0) else 'odd and True' if ((np.square(i)+1)%5 ==0) else 'false' for i in range(10)]
print(str5list)
['odd',
 'false',
 'odd',
 'odd and True',
 'odd',
 'false',
 'odd',
 'odd and True',
 'odd',
 'false']

Top