-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary_looping.py
51 lines (41 loc) · 1.95 KB
/
dictionary_looping.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
user_0 = {'username': 'efermi', 'first': 'enrico', 'last': 'fermi'}
for k, v in user_0.items(): # define two variables (k and v) to be equal to the keys:values in user_0
print(f"Key: {k}")
print(f"Value: {v}")
print()
print()
favorite_languages = {'jen': 'python', 'sarah': 'c', 'edward': 'rust', 'phil': 'python'}
for name, language in favorite_languages.items():
print(f"{name.title()}'s favorite language is {language.title()}.")
print()
print(favorite_languages.keys()) # prints the dictionary keys for favorite_languages
print(favorite_languages.values()) # prints the dictionary values for favorite_languages
print()
for name in favorite_languages.keys(): # sorts the list of keys into name "keys()" is optional
print(name.title())
print()
# still using favorite_languages dictionary from above
friends = ['phil', 'sarah', 'erin'] # "list" of friends
for name in favorite_languages: # looping by keys is the default so ".keys()" is optional
print(f'Hi {name.title()}.')
if name in friends: # check to see if one of friends match dictionary key
language = favorite_languages[name]
print(f'\t{name.title()}, I see you love {language.title()}!')
print()
for name in friends:
if name not in favorite_languages: # check if friend is not in the favorite_languages dictionary
print(f'{name.title()}, you need to take the language poll.')
else: # if fined is in the dictionary
print(f'{name.title()} has took the poll.')
print()
for name in sorted(favorite_languages.keys()): # sorts the keys in favorite_languages before going through them
print(f'{name}')
print()
print('The following languages are in the dictionary:')
for language in favorite_languages.values(): # go through dictionary values instead of keys
print(language)
print()
# set
print('The following languages are in the dictionary:')
for language in set(favorite_languages.values()): # use "set" to remove duplicates
print(language)