-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathL #18 - Comprehension.py
90 lines (72 loc) · 2.87 KB
/
L #18 - Comprehension.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
83
84
85
86
87
88
89
90
# ## Day 15: Comprehensions
# ## Concept: List comprehensions - List comprehensions are used to create a new list from some other iterable.
# # List comprehension with list
# names = ["mary", "Richard", "Noah", "KATE"]
# names = [name.title() for name in names]
# print(names)
# # output: ['Mary', 'Richard', 'Noah', 'Kate']
# # List comprehensions with tuples
# names = ("mary", "Richard", "Noah", "KATE")
# ages = (36, 21, 40, 28)
# info = [(name.title(), age) for name, age in zip(names, ages)]
# print(info)
# # output: [('Mary', 36), ('Richard', 21), ('Noah', 40), ('Kate', 28)]
# ## Concept: Set comprehensions - Works like List comprehensions but it produces a set, rather than a list.
# # Set comprehension with list
# names = ["mary", "Richard", "Noah", "KATE"]
# names = {name.title() for name in names}
# print(names)
# # output: {'Richard', 'Mary', 'Kate', 'Noah'}
# ## Concept: Dictionary comprehensions
# # Dictionary comprehension with tuples
# names = ("Mary", "Richard", "Noah", "Kate")
# ages = (36, 21, 40, 28)
# info = {
# name:age
# for name, age in zip(names,ages)
# }
# print(info)
# # output: {'Mary': 36, 'Richard': 21, 'Noah': 40, 'Kate': 28}
# ## Concept: Comprehensions and scope
# """
# If we use a comprehension like the following, referring to 'name' gives us a NameError, because 'name' isn't defined.
# """
# names = ["Mary", "Richard", "Noah", "Kate"]
# names_lower = [name.lower() for name in names]
# print(name) # NameError
# """
# So, what exactly is going on here?
# The reason for these differences is that comprehensions are actually functions. When we write something like this:
# """
# names = ["Mary", "Richard", "Noah", "Kate"]
# names_lower = [name.lower() for name in names]
# """Behind the scenes, Python is running a function similar to this:"""
# def temp():
# new_list = []
# for name in names:
# new_list.append(name.lower())
# return new_list
# """
# It's just a regular for loop, like we were using before, only this for loop is running inside a function.
# We can now see why the variables inside comprehensions cannot be accessed outside of the comprehension.
# They only exist while the behind-the-scenes function is running, and they're only available inside that function.
# """"
## Exercises:
#1. Convert the following for loop into a comprehension.
numbers = [1, 2, 3, 4, 5]
numbers = [number**2 for number in numbers]
print(numbers)
# output: [1, 4, 9, 16, 25]
#2. Use a dictionary comprehension to create a new dictionary from the dictionary below, where each of the values is title case.
movie = {
"title": "thor: ragnarok",
"director": "taika waititi",
"producer": "kevin feige",
"production_company": "marvel studios"
}
movie = {
key:value.title()
for key, value in movie.items()
}
print(movie)
# output: {'title': 'Thor: Ragnarok', 'director': 'Taika Waititi', 'producer': 'Kevin Feige', 'production_company': 'Marvel Studios'}