I have a dictionary.
prices = {'n': 99, 'a': 99, 'c': 147}
using map () I need to receive new dictionary :
def formula(value):
value = value -value * 0.05
return value
new_prices = dict(map(formula, prices.values()))
but it doesn't work
TypeError: cannot convert dictionary update sequence element #0 to a sequence
solving my code using map():
new_prices = {'n': 94.05, 'a': 94.05, 'c': 139.65}
you can do this using zip and map
new_prices = dict(zip(prices, map(formula, prices.values())))
Use dictionary comprehension:
new_prices = {k: formula(prices[k]) for k in prices}
print(new_prices)
# {'n': 94.05, 'a': 94.05, 'c': 139.65}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With