-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdict programs practice.txt
More file actions
44 lines (37 loc) · 1.49 KB
/
dict programs practice.txt
File metadata and controls
44 lines (37 loc) · 1.49 KB
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
def email_list(domains):
emails = []
for domain, users in domains.items():
for user in users:
emails.append("{}@{}". format(user,domain))
return(emails)
print(email_list({"gmail.com": ["clark.kent", "diana.prince", "peter.parker"], "yahoo.com": ["barbara.gordon", "jean.grey"], "hotmail.com": ["bruce.wayne"]}))
def groups_per_user(group_dictionary):
user_groups = {}
# Go through group_dictionary
for group, users in group_dictionary.items():
for user in users:
if user not in user_groups:
user_groups[user] = []
user_groups[user].append(group)
#user_groups[user].append(group)
# Now add the group to the the list of
# groups for this user, creating the entry
# in the dictionary if necessary
return(user_groups)
print(groups_per_user({"local": ["admin", "userA"],
"public": ["admin", "userB"],
"administrator": ["admin"] }))
def add_prices(basket):
# Initialize the variable that will be used for the calculation
total = 0
# Iterate through the dictionary items
for item, price in basket.items():
# Add each price to the total calculation
# Hint: how do you access the values of
# dictionary items?
total += price
# Limit the return value to 2 decimal places
return round(total, 2)
groceries = {"bananas": 1.56, "apples": 2.50, "oranges": 0.99, "bread": 4.59,
"coffee": 6.99, "milk": 3.39, "eggs": 2.98, "cheese": 5.44}
print(add_prices(groceries)) # Should print 28.44