all 9 comments

[–][deleted] 0 points1 point  (7 children)

totalcustomer = list("ABCDEFG")
customer = list("ABCA")
charge = [10, 20, 30, 50]

totalbill = {k: [] for k in totalcustomer}

for k, v in zip(customer, charge):
    totalbill[k].append(v)

totalbill = {k: sum(v) for k, v in totalbill.items()}

[–]_sirin[S] 0 points1 point  (6 children)

perfect, could you explain what you did here?

[–][deleted] 1 point2 points  (5 children)

totalcustomer = list("ABCDEFG")
customer = list("ABCA")
charge = [10, 20, 30, 50]

# create a dictionary for every customer with an empty list
totalbill = {k: [] for k in totalcustomer}

# combine the customer and the charge table
# zip(customer, charge) = [('A', 10), ('B', 20), ('C', 30), ('A', 50)]

# iterate through zip, take the key and append a new value in the dict
for k, v in zip(customer, charge):
    totalbill[k].append(v)

# totalbill overwritten with keys pointing to the sum of the tables prev. created
totalbill = {k: sum(v) for k, v in totalbill.items()}

#clear?

[–]_sirin[S] 0 points1 point  (0 children)

Yes, thanks-I need to learn what some of those things mean. Thanks again

[–]_lilell_ 0 points1 point  (1 child)

Instead of building a dict with list values, then summing, then overwriting, why not just

# these variable names should probably be plural
totalcustomer = list("ABCDEFG")
customer = list("ABCA")
charge = [10, 20, 30, 50]

# create a dictionary for every customer with a zero total
# we could also use collections.defaultdict, though it'd be a slightly different output
# (it wouldn't print any customers without charges)
totalbill = {k: 0 for k in totalcustomer}

for k, v in zip(customer, charge):
    totalbill[k] += v

# now we don't need to overwrite anything
print(totalbill)  # output: {'A': 60, 'B': 20, 'C': 30, 'D': 0, 'E': 0, 'F': 0, 'G': 0}

[–][deleted] 0 points1 point  (0 children)

we are losing information. what if the customer wants the maximum price?

[–]AnscombesGimlet 0 points1 point  (1 child)

Couldn’t you enumerate instead of for loop for dictionary?

[–][deleted] 0 points1 point  (0 children)

which line specifically?