I have two dictionaries:
AvailConcertSeats = {"The Weeknd": 80000, "Maroon 5": 80000, "Justin Bieber": 80000, "Post Malone": 34000}
AvgConcertPrice = {"The Weeknd": 200, "Maroon 5": 109, "Justin Bieber": 250, "Post Malone": 179}
I'd like to print out the multiplication of AvailConcertSeats
values x AvgConcertPrice
values.
Code I've tried:
AvailConcertSeats.values() * AvgConcertPrice.values()
CodePudding user response:
You can use dict comprehension:
AvailConcertSeats = {"The Weeknd": 80000, "Maroon 5": 80000, "Justin Bieber": 80000, "Post Malone": 34000}
AvgConcertPrice = {"The Weeknd": 200, "Maroon 5": 109, "Justin Bieber": 250, "Post Malone": 179}
output = {k: seats * AvgConcertPrice[k] for k, seats in AvailConcertSeats.items()}
print(output)
# {'The Weeknd': 16000000, 'Maroon 5': 8720000, 'Justin Bieber': 20000000, 'Post Malone': 6086000}
If you want to be "safe" (when the keys of the two dicts are possibly not identical), the following will only calculate on the intersection of them:
output = {k: AvailConcertSeats[k] * AvgConcertPrice[k] for k in AvailConcertSeats.keys() & AvgConcertPrice.keys()}