-
Notifications
You must be signed in to change notification settings - Fork 0
/
gryffindors.py
83 lines (50 loc) · 1.93 KB
/
gryffindors.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# ## Calling out students from gryffindor from our dictionary
# students = [
# {"name": "Hermione", "house": "Gryffindor"},
# {"name": "Harry", "house": "Gryffindor"},
# {"name": "Ron", "house": "Gryffindor"},
# {"name": "Draco", "house": "Slytherin"},
# {"name": "Padma", "house": "Ravenclaw"},
# ]
# gryffindors = [
# student["name"] for student in students if student["house"] == "Gryffindor"
# ]
# for gryffindor in sorted(gryffindors):
# print(gryffindor)
# ## Approaching code above using 'filter'
# students = [
# {"name": "Hermione", "house": "Gryffindor"},
# {"name": "Harry", "house": "Gryffindor"},
# {"name": "Ron", "house": "Gryffindor"},
# {"name": "Draco", "house": "Slytherin"},
# {"name": "Padma", "house": "Ravenclaw"},
# ]
# def is_gryffindor(s):
# return s["house"] == "Gryffindor"
# gryffindors = filter(is_gryffindor, students)
# for gryffindor in sorted(gryffindors, key=lambda s: s["name"]):
# print(gryffindor["name"])
## Using 'dictionary comprehensions
# ## Creating dictionaries "on the fly"
# students = ["Hermione", "Harry", "Ron"]
# gryffindors =[]
# for student in students:
# gryffindors.append({"name": student, "house": "Gryffindor"})
# print(gryffindors)
# ## More simplified from code above
# students = ["Hermione", "Harry", "Ron"]
# gryffindors = [{"name": student, "house": "Gryffindor"} for student in students]
# print(gryffindors)
# ## What if I wanted only one big dictinary? == dictionary comprehension application
# students = ["Hermione", "Harry", "Ron"]
# gryffindors = {student: "Gryffindor" for student in students}
# print(gryffindors)
## Adding a numeration into our list
# ## One approach
# students = ["Hermione", "Harry", "Ron"]
# for i in range(len(students)):
# print(i + 1, students[i])
## 'enumerate' approach
students = ["Hermione", "Harry", "Ron"]
for i, student in enumerate(students):
print(i + 1, student)