List Comprehensions in Python: A Comprehensive Guide, Study notes of Web Programming and Technologies

List comprehensions provide a more compact and ... expr is Python expression, typically involving the variable x. ... Nested List Comprehensions. Example:.

Typology: Study notes

2022/2023

Uploaded on 03/01/2023

anamika
anamika 🇺🇸

4.7

(16)

254 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MARCH 24TH, 2014
List Comprehensions
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download List Comprehensions in Python: A Comprehensive Guide and more Study notes Web Programming and Technologies in PDF only on Docsity!

M A R C H 2 4 T H^ , 2 0 1 4

List Comprehensions

Examples to Get Us Started

— [x**2 for x in range(10)] [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] — [str(x)+str(x) for x in range(10)] ['00', '11', '22', '33', '44', '55', '66', '77', '88', '99'] — [str(x)+str(x) for x in range(10) if x%2 == 0] ['00', '22', '44', '66', '88']

List Comprehension: Basic Syntax

[ expr for x in list] Notes: — for and in are Python keywords, used just as in for-loops. — x is a variable that takes on values of elements in list, in order. — expr is Python expression, typically involving the variable x. — The expression [ expr for x in list] evaluates to a list made up of the different values that expr takes on for different x. — This is similar to the “set builder” notation used in math: {x*y | x and y are even}.

List Comprehensions: Syntax with if-clause

[ expr for x in list if bool-expr ]

Notes:

— bool-expr is a boolean expression involving x.

— The overall expression evaluates to a list of values of

expr evaluated for all values of x in list satisfying the

bool-expr.

— Example: [str(x)+str(x) for x in range(10) if x%2 == 0]

evaluates to ['00', '22', '44', '66', '88']

Nested List Comprehensions

Example: [x*y for x in range(3) for y in range(3)] [0, 0, 0, 0, 1, 2, 0, 2, 4] Notes:

— As in nested loops, for every iteration of the first loop

(the for-x loop), all iterations of the second loop (the

for-y loop) are executed.

Example: Generating Perfect Squares

[x for x in range(100) for y in range(x) if y*y == x]

[4, 9, 16, 25, 36, 49, 64, 81]

Notes:

— Those x and y values (from their respective lists) that

satisfy the condition y

2

= x, are generated.

— Thus all x values generated in this manner are

perfect squares.

Example: Generating Prime Numbers

primes = [x for x in range(2, 100) if x not in composites] Notes: — Primes in the range 2..99 can be obtained by taking the complement of the generated composites.

Example: Flattening Lists

nestedList = [range(x) for x in range(1, 4)] nestedList [[0], [0, 1], [0, 1, 2]] [y for x in nestedList for y in x] [0, 0, 1, 0, 1, 2]

Warning!

— The danger with list comprehensions is that your

code may become hard to understand, especially

with nested list comprehensions.

— If by using a list comprehension, you are making

your code hard to understand, then it is time to

desist.