Been informed this has been answered - apoligies Thank you!
Asked
Active
Viewed 51 times
-5
-
1Does this answer your question? [Dictionary Comprehension in Python 3](https://stackoverflow.com/questions/20489609/dictionary-comprehension-in-python-3) – sal Sep 18 '20 at 01:49
3 Answers
0
Since you wanted a function to perform this:
def newprice(d):
return {"all" + key: value * 5 for key, value in d.items()}
# To be used as :
dict1 = {'apple':5, 'banana':4}
print(newprice(dict1))

sal
- 3,515
- 1
- 10
- 21
0
In dictionary comprehension you can do whatever you want to keys and values:
def newprice(d):
return {f'all{k}': v * 5 for k, v in d.items()}

Ali Tou
- 2,009
- 2
- 18
- 33
0
Here you go -
dict1 = {'apple':5, 'banana':4}
dict2 = {('all'+ k): 5*dict1[k] for k in dict1 }
print(dict2)

DS_
- 247
- 2
- 10