PY
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
#import this
#There should be one-- and preferebly only one --obvious way to do it.
#Creating a list of all odd numbers between 0 and 20
#When I started with Python - coming from a C background, this was how I would do it
filtered_list1=[0]*10
j=0
for i in range(21):
if i%2==1:
filtered_list1[j]=i
j+=1
print(filtered_list1)
#As a Python Novice this would be my answer
filtered_list2=[]
for i in range(21):
if i%2:
filtered_list2.append(i)
print(filtered_list2)
#Then I learned about lambda, filter and map, and would be quite impressed with this solution.
filtered_list3=list(filter(lambda x:x%2,range(21)))
print(filtered_list3)
#Then I learned of list comprehension and that Guido favours it
filtered_list4=[i for i in range(21) if i%2]
print(filtered_list4)
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run