-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgryffindors.py
More file actions
64 lines (51 loc) · 1.4 KB
/
gryffindors.py
File metadata and controls
64 lines (51 loc) · 1.4 KB
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
students = [
{"name": "Hermione", "house": "Gryffindor"},
{"name": "Harry", "house": "Gryffindor"},
{"name": "Ron", "house": "Gryffindor"},
{"name": "Draco", "house": "Slytherin"},
]
'''
# list comprehensions
gryffindors = [
student["name"] for student in students if student["house"] == "Gryffindor"
]
for gryffindor in sorted(gryffindors):
print(gryffindor)
'''
'''
def is_gryffindor(s):
#if s["house"] == "Gryffindor":
# return True
#else:
# return False
return s["house"] == "Gryffindor"
'''
'''
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"])
'''
'''
# dictionary comprehensions
students = ["Hermione", "Harry", "Ron"]
gryffindors = []
for student in students:
gryffindors.append({"name": student, "house": "Gryffindor"})
print(gryffindors)
'''
'''
students = ["Hermione", "Harry", "Ron"]
gryffindors = [{"name": student, "house": "Gryffindor"} for student in students]
print(gryffindors)
'''
'''
# dictionary comprehensions
students = ["Hermione", "Harry", "Ron"]
gryffindors = {student: "Gryffindor" for student in students}
print(gryffindors)
'''
students = ["Hermione", "Harry", "Ron"]
for i in range(len(students)):
print(i + 1, students[i])