List comprehensions

This commit is contained in:
Josh Mudge 2019-01-26 21:05:56 -07:00
parent 8bd574b39c
commit 65264ff543
1 changed files with 12 additions and 0 deletions

View File

@ -282,6 +282,18 @@ orders.append('tulips')
You can add lists like this: `['list1', 'stuff'] + ['list2', 'stuff']` but need to use `lst1 + lst2` and not `[lst1] + [lst2]`
# List Comprehensions
Say you only want to allow people over the height of 161 to ride your roller coaster for safety reasons. You could do this to create a list of only those heights about 161.
```
heights = [161, 164, 156, 144, 158, 170, 163, 163, 157]
can_ride_coaster = []
can_ride_coaster = [height for height in heights if height > 161]
print(can_ride_coaster)
```
## Ranges
You can get a range of numbers using `range()`