0% found this document useful (0 votes)
20 views8 pages

List Comprehensions

This has notes about List comprehensions in python programming

Uploaded by

Swaty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views8 pages

List Comprehensions

This has notes about List comprehensions in python programming

Uploaded by

Swaty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Quick Recap

You should already know what list comprehensions are, but let's quickly recap their syntax
and how they work:

goal → generate a list by transforming, and optionally filtering, another iterable

• start with some iterable other_list = ['this', 'is', 'a', 'parrot']

• create empty new list new_list = []

• iterate over the original iterable for item in other_list:

• skip over certain values (filter) if len(item) > 2:

• transform value and append to new list new_list.append(item[::-1])

List comprehension:
new_list = [item[::-1] for item in other_list if len(item) > 2]

transformation iteration filter


Formatting the Comprehension Expression

If the comprehension expression gets too long, it can be split over multiple lines

For example, let's say we want to create a list of squares of all the integers
between 1 and 100 that are not divisible by 2, 3 or 5

sq = [i**2 for i in range(1, 101) if i%2 and i%3 and i%5]

We could write this over multiple lines:

sq = [i**2
for i in range(1, 101)
if i%2 and i%3 and i%5]
Nested Comprehensions

Comprehensions can be nested within each other

And since they are functions, a nested comprehension can access (nonlocal) variables from the
enclosing comprehension!

[ [i * j for j in range(5)] for i in range(5)]


closure
nested comprehension local variable: j
free variable: i
outer comprehension
local variable: i
Nested Loops in Comprehensions

We can have nested loops (as many levels as we want) in comprehensions.

This is not the same as nested comprehensions

l = []
for i in range(5):
for j in range(5):
for k in range(5):
l.append((i, j, k))

l = [(i, j, k) for i in range(5) for j in range(5) for k in range(5)]

Note that the order in which the for loops are specified in the comprehension
correspond to the order of the nested loops
Nested Loops in Comprehensions

Nested loops in comprehensions can also contain if statements

Again the order of the for and if statements does matter, just like a normal set of for
loops and if statements
won't work!
l = [] l = []
for i in range(5): for i in range(5):
for j in range(5): if i==j:
if i==j: for j in range(5):
l.append((i, j)) l.append((i, j))

j is referenced after
j is created here
it has been created
l = [(i, j) for i in range(5) for j in range(5) if i == j]

l = [(i, j) for i in range(5) if i == j for j in range(5)]

won't work!
Nested Loops in Comprehensions

l = []
for i in range(1, 6):
[(i, j)
if i%2 == 0:
for i in range(1, 6) if i%2==0
for j in range(1, 6):
for j in range(1, 6) if j%3==0]
if j%3 == 0:
l.append((i,j))

l = []
[(i, j)
for i in range(1, 6):
for i in range(1, 6)
for j in range(1, 6):
for j in range(1, 6)
if i%2==0:
if i%2==0
if j%3 == 0:
if j%3==0]
l.append((i,j))

l = []
[(i, j)
for i in range(1, 6):
for i in range(1, 6)
for j in range(1, 6):
for j in range(1, 6)
if i%2==0 and j%3==0:
if i%2==0 and j%3==0]
l.append((i,j))
Code
Exercises

You might also like