Here is the code(Running Python 2.7.6):
currency_pairs = {'PPC': 10}
print currency_pairs
currency_pairs = currency_pairs.update({'NMC': 50})
print type (currency_pairs)
Output:
{'PPC': 10}
<type 'NoneType'>
Why won't Python add to the dictionary? I don't understand this. Any thoughts?
The method update
just updates the dictionary but don't return anything (in other words, returns None
) In the line
currency_pairs = currency_pairs.update({'NMC': 50})
you are assigning None
to currecy_pairs
. The method itself, will modify the dictionary, so you should call it like this:
currency_pairs = {'PPC': 10}
print currency_pairs
currency_pairs.update({'NMC': 50})
print currency_pairs
Output:
{'PPC': 10}
{'PPC': 10, 'NMC': 50}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.