Concerning Dictionaries and += in Python 2.x

Peter Otten __peter__ at web.de
Wed Jan 21 09:59:58 CET 2015


Peter Otten wrote:

> Denis McMahon wrote:

>>     sold = {k:0 for k in shopping.keys()}
> 
> There is also dict.from_keys()

Sorry, fromkeys():

>>> shopping = {'orange': 5, 'pear': 5, 'banana': 5, 'apple': 4}
>>> dict.fromkeys(shopping, 0)
{'banana': 0, 'orange': 0, 'apple': 0, 'pear': 0}





More information about the Python-list mailing list