From 65264ff543bd1fd3fd98fa4140c6cf82a721d9e5 Mon Sep 17 00:00:00 2001 From: Josh Mudge Date: Sat, 26 Jan 2019 21:05:56 -0700 Subject: [PATCH] List comprehensions --- Codecademy.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Codecademy.md b/Codecademy.md index a7d9e34..30ff049 100644 --- a/Codecademy.md +++ b/Codecademy.md @@ -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()`