-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dictionaries.py
100 lines (65 loc) · 2.11 KB
/
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
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
#Dictionaries, dictionaries keeps the order of the elements
alien_0 = {'color': 'green','points': 5}
print(alien_0['color'])
print(alien_0['points'])
# Setup coordinates
alien_0['x_position'] = 0
alien_0['y_position'] = 25
print(alien_0)
alien_1 = {'color': 'green'}
print(f"The alien color is {alien_1['color']}")
alien_1['color']='yellow'
print(f"The alien color is now {alien_1['color']}")
# Removing key-value pairs from dictionary:
del alien_0['points']
print(alien_0)
#Multi line dictionary:
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}
print(favorite_languages)
# Sarah's favorite language
language = favorite_languages['sarah'].title()
print(f"Sarah's favorite language {language}")
#Access if key doesn't exist
#print(alien_0['points'])
# output KeyError: 'points'
#Use get method to return default value if the requested value doesn't exist..
print(alien_0.get('points',0))
print(alien_0.get('points')) # Returns None without second argument
#Iteration through Dictionary:
user_0 = {
'username': 'efermi',
'first': 'enrico',
'lastname': 'fermi',
}
for k, v in user_0.items():
print(f"\nKey: {k}")
print(f"Value: {v}")
#Just names of the people who took poll for the language:
for name in favorite_languages.keys():
print(name.title())
for name in favorite_languages:
print(name.title())
#Looping through dictionary's keys in particular order
for name in sorted(favorite_languages.keys()):
print(f"{name.title()}, thank you for taking the poll.")
#Looping through the dictionary's values
print("\n")
for language in favorite_languages.values():
print(language.title())
#values may have duplicates, to get the unique elements from values, we need to convert values to set
# A set is a collection in which each item must be unique.
print("\nUnique languages: ")
for language in set(favorite_languages.values()):
print(language.title())
# A list of dictionaries
alien0 = {'color': 'green', 'points': 5}
alien1 = {'color': 'yellow', 'points': 10}
alien2 = {'color': 'red', 'points': 15}
aliens = [alien0, alien1, alien2]
for alien in aliens:
print(alien)