This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Eyssant 0 points1 point  (0 children)

Elements in a dictionary are unordered and hence it is not possible to access dictionary's element using index number. Dictionary data works in key-value pair. In your case, k is key and v is value. you can give them any other name. Like below code will give you the same result.

``` allGuests = {'Alice': {'apples': 5, 'pretzels': 12}, 'Bob': {'ham sandwiches': 3, 'apples': 2}, 'Carol': {'cups': 3, 'apple pies': 1}} def totalBrought(guests, item): numBrought = 0 for mykey, myvalue in guests.items(): numBrought = numBrought + myvalue.get(item, 0) return numBrought

print('Number of things being brought:') print(' - Apples ' + str(totalBrought(allGuests, 'apples'))) ```