# 1. List Comprehensions

See docs at: https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions See ref video at: https://www.youtube.com/watch?v=3dt4OGnU5sM

## 1.1. List comprehension

Syntax:

- new_list = [expression for item in iterable]
- Parameters:
**expression**– the item variable only (e.g. x) or any expression such as one that uses the item variable (e.g. x * x).**item**– a variable.**iterable**– iterable objects like strings, lists, dictionaries, range function and others.

The first 2 examples illustrate simple list comprehensions without doing anything with the values.

### 1.1.1. List Example: list

```
nums = [1, 3, 6, 10, 15, 21, 28]
# I want 'n' for each 'n' in nums
my_list = []
for n in nums:
my_list.append(n)
print(my_list)
```

`my_list_comprehension = [n for n in nums]`

, does this in one line.```
nums = [1, 3, 6, 10, 15, 21, 28]
my_list_comprehension = [n for n in nums]
print(my_list_comprehension)
```

### 1.1.2. List Example: range

```
# I want 'n' for each 'n' from 1 to 10
my_list = []
for n in range(1, 11):
my_list.append(n)
print(my_list)
```

`my_list_comprehension = [n for n in range(1, 11)]`

, does this in one line.```
my_list_comprehension = [n for n in range(1, 11)]
print(my_list_comprehension)
```

### 1.1.3. List Example: n * n

```
nums = [1, 3, 6, 10, 15, 21, 28]
# I want 'n*n' for each 'n' in nums
my_list = []
for n in nums:
my_list.append(n * n)
print(my_list)
```

`my_list_comprehension = [n * n for n in nums]`

, does this in one line.```
nums = [1, 3, 6, 10, 15, 21, 28]
my_list_comprehension = [n * n for n in nums]
print(my_list_comprehension)
```

### 1.1.4. Practice Questions

Tasks

Use a list comprehension to create a list of 2 * n for each n in [0, 1, 1, 2, 3, 5, 8].

Use a list comprehension to create a list of 2 * n - 1 for each n in [2, 4, 6, 8, 10].

Use a list comprehension to create a list of n ** 3 for each n in [1, 2, 3, 4].

## 1.2. List comprehension: conditions

Syntax:

- new_list = [expression for item in iterable if condition == True]
- Parameters:
**expression**– the item variable only (e.g. x) or any expression such as one that uses the item variable (e.g. x*x, x.upper()).**item**– variable.**iterable**– iterable objects like strings, lists, dictionaries, range function and others.**condition**– any condition.

### 1.2.1. List Example: Modulo

`n % 2`

, returns 0 for even numbers.```
nums = [1, 3, 6, 10, 15, 21, 28]
# I want n for each n in nums if n is even
my_list = []
for n in nums:
if n % 2 == 0:
my_list.append(n)
print(my_list)
```

`my_list_comprehension = [n for n in nums if n % 2 == 0]`

, does this in one line.```
nums = [1, 3, 6, 10, 15, 21, 28]
# I want n for each n in nums if n is even
my_list_comprehension = [n for n in nums if n % 2 == 0]
print(my_list_comprehension)
```

### 1.2.2. Practice Questions

Tasks

Use a list comprehension to create a list of even n for each n in [1, 2, 3, 5, 8].

Use a list comprehension to create a list of odd n for each n in [1, 2, 3, 5, 8].

## 1.3. List comprehension: nested

Syntax:

- new_list = [expression for item1 in iterable1 for item2 in iterable2]
- Parameters:
**expression**– an expression using item1 and item2.**item2**– variable for iterable1.**iterable1**– iterable objects like strings, lists, dictionaries, range function and others.**item2**– variable for iterable2.**iterable2**– a second iterable objects like strings, lists, dictionaries, range function and others.

### 1.3.1. List Example: grid coordinates

```
# I want a (letter, num) pair for each letter in 'ABCD' and each number in '1234'
my_list = []
for letter in "ABCD":
for num in range(1, 5):
my_list.append((letter, num))
print(my_list)
```

```
my_list_comprehension = [(letter, num) for letter in "ABCD" for num in range(1, 5)]
print(my_list_comprehension)
```

### 1.3.2. Practice Questions

Tasks

Starting with two lists, [‘A’, ‘B’], [‘X’, ‘Y’], use a list comprehension to create the list: [(‘A’, ‘X’), (‘A’, ‘Y’), (‘B’, ‘X’), (‘B’, ‘Y’)].

Starting with two lists, [‘A’, ‘B’], [‘X’, ‘Y’], use a list comprehension to create the list: [‘AvX’, ‘AvY’, ‘BvX’, ‘BvY’].

### 1.3.3. Summary Examples

```
my_list_comprehension = [n for n in nums]
my_list_comprehension = [n for n in range(1, 11)]
my_list_comprehension = [n * n for n in nums]
my_list_comprehension = [n for n in nums if n % 2 == 0]
my_list_comprehension = [(letter, num) for letter in "ABCD" for num in range(1, 5)]
```

Set Comprehension on a List primes = [2, 2, 2, 3, 3, 5, 5, 5, 7, 11, 11, 13, 13, 13, 13] primes_squared = {p*p for p in primes} print(primes_squared)

More Complex Expressions: quadratic transformation Any expression is allowed. More complex expressions can be put in parentheses. Here, quadratic equation: 2x^2 + 5x + 10

transformed = {(2*x*x + 5*x + 10) for x in primes} print(transformed)

### 1.3.4. Making a 2D array from a list

`for i in range(0, len(nums), x)`

, becomes `for i in range(0, 6, 3)`

which has i values of 0 and 3.`for j in range(i, i+x)`

, becomes `for j in range(0, 3)`

when i is 0, and `for j in range(3,6)`

when i is 3```
nums = [1, 3, 5, 7, 9, 11]
x = 3
list_2d = []
for i in range(0, len(nums), x):
rowlist = []
for j in range(i, i+x):
rowlist.append(nums[j])
list_2d.append(rowlist)
print(list_2d)
# [[1, 3, 5], [7, 9, 11]]
```

```
nums = [1, 3, 5, 7, 9, 11]
x = 3
list_2d = [nums[i:i+x] for i in range(0, len(nums), x)]
print(list_2d)
# [[1, 3, 5], [7, 9, 11]]
```