forked from makersacademy/intro-to-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
036_filtering.py
47 lines (32 loc) · 1.2 KB
/
036_filtering.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Video alternative: https://vimeo.com/954334322/c5a36d4407#t=214
from lib.helpers import check_that_these_are_equal
# Filtering is going through a list and keeping only some of
# the items, typically according to a condition of some
# kind. This is useful when you only want to keep some of
# the items in your list.
# For example:
# * Filtering a list of products to only items under £5
# * Filtering a list of numbers to remove minus numbers
# * Removing 'junk' data from a list
# Here's an example:
# Imagine someone didn't put their age in
raw_ages = [32, 40, None, 1, 32]
clean_ages = [] # This is our accumulator again
for age in raw_ages: # We go through each age
# We combine a for with an if to remove 'None' items
if age != None:
clean_ages.append(age)
print(raw_ages)
print(clean_ages)
# @TASK: run this program to see what it does.
# @TASK: Complete this exercise.
print("")
print("Function: only_positive_numbers")
# Return a new list with only the positive numbers
def only_positive_numbers(numbers):
pass
check_that_these_are_equal(
only_positive_numbers([-4, 4, -3, 3]), [4, 3])
check_that_these_are_equal(
only_positive_numbers([-100]), [])
# When you're done, move on to 037_dicts.py