-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionaries_loop.py
More file actions
41 lines (29 loc) · 885 Bytes
/
Dictionaries_loop.py
File metadata and controls
41 lines (29 loc) · 885 Bytes
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
# Python - Loop Dictionaries
# Loop Through a Dictionary
# You can loop through a dictionary by using a for loop.
# When looping through a dictionary, the return value are the keys of the dictionary, but there are methods to return the values as well.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
# Example
# Print all key names in the dictionary, one by one:
for x in thisdict:
print(x)
# Example
# Print all values in the dictionary, one by one:
for x in thisdict:
print(thisdict[x])
# Example
# You can also use the values() method to return values of a dictionary:
for x in thisdict.values():
print(x)
# Example
# You can use the keys() method to return the keys of a dictionary:
for x in thisdict.keys():
print(x)
# Example
# Loop through both keys and values, by using the items() method:
for x, y in thisdict.items():
print(x, y)