What is a list comprehension?

Practicing Coding Interview Questions in Python

Kirill Smirnov

Data Science Consultant, Altran

List comprehension

nums = [2, 4, 6, 8, 10]
print(nums)
[2, 4, 6, 8, 10]
nums_new = []
for i in range(1, 6):
    nums_new.append(2*i)

print(nums_new)
[2, 4, 6, 8, 10]
Practicing Coding Interview Questions in Python

List comprehension

nums = [2, 4, 6, 8, 10]
print(nums)
[2, 4, 6, 8, 10]
                      for num in range(1, 6)
Practicing Coding Interview Questions in Python

List comprehension

nums = [2, 4, 6, 8, 10]
print(nums)
[2, 4, 6, 8, 10]
           [          for num in range(1, 6)]
Practicing Coding Interview Questions in Python

List comprehension

nums = [2, 4, 6, 8, 10]
print(nums)
[2, 4, 6, 8, 10]
           [(2 * num) for num in range(1, 6)]
Practicing Coding Interview Questions in Python

List comprehension

nums = [2, 4, 6, 8, 10]
print(nums)
[2, 4, 6, 8, 10]
nums_new = [(2 * num) for num in range(1, 6)]
print(nums_new)
[2, 4, 6, 8, 10]
Practicing Coding Interview Questions in Python

Summing up

List comprehension is defined by:

 

Practicing Coding Interview Questions in Python

Summing up

List comprehension is defined by:

  • an iterable object (e.g. list, tuple, set)

 

[          for num in range(1, 6)]
Practicing Coding Interview Questions in Python

Summing up

List comprehension is defined by:

  • an iterable object (e.g. list, tuple, set)
  • an operation on an element

 

[(2 * num) for num in range(1, 6)]

 

  • (optional) conditions
Practicing Coding Interview Questions in Python

List comprehension with condition

nums = [2, 4, 6, 8, 10]
print(nums)
[2, 4, 6, 8, 10]

 

1 2 3 4 5 6 7 8 9 10

Practicing Coding Interview Questions in Python

List comprehension with condition

nums = [2, 4, 6, 8, 10]
print(nums)
[2, 4, 6, 8, 10]

 

1 2 3 4 5 6 7 8 9 10 $\rightarrow$ 2 4 6 8 10

Practicing Coding Interview Questions in Python

Adding a condition

nums_new = [num for num in range(1, 11)]
print(nums_new)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Practicing Coding Interview Questions in Python

Adding a condition

nums_new = [num for num in range(1, 11) if num % 2 == 0]
print(nums_new)
[2, 4, 6, 8, 10]
Practicing Coding Interview Questions in Python

More examples

text = 'list COMPREHENSION is A way TO create LISTS'

Task:
Create a list that contains the length of each lowercased word.

list, is, way, create $\rightarrow$ [4, 2, 3, 6]

Practicing Coding Interview Questions in Python

More examples

text = 'list COMPREHENSION is A way TO create LISTS'

Task:
Create a list that contains the length of each lowercased word.

list, is, way, create $\rightarrow$ [4, 2, 3, 6]

output = [          for word in text.split()                  ]
Practicing Coding Interview Questions in Python

More examples

text = 'list COMPREHENSION is A way TO create LISTS'

Task:
Create a list that contains the length of each lowercased word.

list, is, way, create $\rightarrow$ [4, 2, 3, 6]

output = [          for word in text.split() if word.islower()]
Practicing Coding Interview Questions in Python

More examples

text = 'list COMPREHENSION is A way TO create LISTS'

Task:
Create a list that contains the length of each lowercased word.

list, is, way, create $\rightarrow$ [4, 2, 3, 6]

output = [len(word) for word in text.split() if word.islower()]
print(output)
[4, 2, 3, 6]
Practicing Coding Interview Questions in Python

Multiple loops

numbers = [1, 2, 3]
letters = ['a', 'b', 'c']

Create all the possible pairs between numbers and letters:

[
    (1, 'a'), (1, 'b'), (1, 'c'),
    (2, 'a'), (2, 'b'), (2, 'c'),
    (3, 'a'), (3, 'b'), (3, 'c'),
]
Practicing Coding Interview Questions in Python

Iterating through multiple loops

numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
pairs = [       for i in numbers                 ]
Practicing Coding Interview Questions in Python

Iterating through multiple loops

numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
pairs = [       for i in numbers for j in letters]
Practicing Coding Interview Questions in Python

Iterating through multiple loops

numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
pairs = [(i, j) for i in numbers for j in letters]
print(pairs)
[
    (1, 'a'), (1, 'b'), (1, 'c'),
    (2, 'a'), (2, 'b'), (2, 'c'),
    (3, 'a'), (3, 'b'), (3, 'c'),
]
Practicing Coding Interview Questions in Python

Deeper look

numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
pairs = [(i, j) for i in numbers for j in letters]
pairs = []
for i in numbers:
    for j in letters:
        pairs.append((i, j))
Practicing Coding Interview Questions in Python

Deeper look

numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
pairs = [(i, j)                  for j in letters]
pairs = []

    for j in letters:
        pairs.append((i, j))
Practicing Coding Interview Questions in Python

Deeper look

numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
pairs = [(i, j) for i in numbers                 ]
pairs = []
for i in numbers:

        pairs.append((i, j))
Practicing Coding Interview Questions in Python

Adding square brackets

pairs = [ (i, j) for i in numbers  for j in letters]
Practicing Coding Interview Questions in Python

Adding square brackets

pairs = [[(i, j) for i in numbers] for j in letters]
print(pairs)
[
    [(1, 'a'), (2, 'a'), (3, 'a')],
    [(1, 'b'), (2, 'b'), (3, 'b')],
    [(1, 'c'), (2, 'c'), (3, 'c')]
]
Practicing Coding Interview Questions in Python

Adding square brackets

pairs = [[(i, j) for i in numbers] for j in letters]
pairs = []
for j in letters:
    temp = []
    for i in numbers:
        temp.append((i, j))
    pairs.append(temp)
Practicing Coding Interview Questions in Python

Adding square brackets

pairs = [[(i, j) for i in numbers]                 ]
pairs = []

    temp = []
    for i in numbers:
        temp.append((i, j))
    pairs.append(temp)
Practicing Coding Interview Questions in Python

Adding square brackets

pairs = [[(i, j)                 ] for j in letters]
pairs = []
for j in letters:
    temp = []

        temp.append((i, j))
    pairs.append(temp)
Practicing Coding Interview Questions in Python

Swap numbers and letters

numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
pairs = [[(i, j) for i in numbers] for j in letters]
print(pairs)
[
    [(1, 'a'), (2, 'a'), (3, 'a')],
    [(1, 'b'), (2, 'b'), (3, 'b')],
    [(1, 'c'), (2, 'c'), (3, 'c')]
]        
Practicing Coding Interview Questions in Python

Swap numbers and letters

numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
pairs = [[(i, j) for i in letters] for j in numbers]
print(pairs)
[
    [('a', 1), ('b', 1), ('c', 1)],
    [('a', 2), ('b', 2), ('c', 2)],
    [('a', 3), ('b', 3), ('c', 3)]
]        
Practicing Coding Interview Questions in Python

Difference between list comprehensions

numbers = [1, 2, 3]
letters = ['a', 'b', 'c']
pairs = [(i, j) for i in numbers for j in letters]
pairs = [[(i, j) for i in numbers] for j in letters]
pairs = [[(i, j) for i in letters] for j in numbers]
Practicing Coding Interview Questions in Python

Let's practice!

Practicing Coding Interview Questions in Python

Preparing Video For Download...