-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path6_loop_dictionaries.py
31 lines (26 loc) · 981 Bytes
/
6_loop_dictionaries.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
# Python - Loop Dictionaries
# Loop Through a Dictionary
# You can loop through a dictionary by using a for loop.
thisdict = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
for x in thisdict:
print(x)
print('----------------------------------------------------------------')
# Print all values in the dictionary, one by one.
for x in thisdict:
print(thisdict[x])
print('----------------------------------------------------------------')
# You can also use the values() method to return values of a dictionary.
for x in thisdict.values():
print(x)
print('----------------------------------------------------------------')
# You can use the keys() method to return the keys of a dictionary.
for x in thisdict.keys():
print(x)
print('----------------------------------------------------------------')
# Loop through both keys and values, by using the items() method.
for x, y in thisdict.items():
print(str(x) + "->" + str(y))