Appending to a list, which is value of a dictionary

Chris Angelico rosuav at gmail.com
Sat Oct 15 12:07:25 EDT 2016


On Sat, Oct 15, 2016 at 11:35 PM, Uday J <udayj2121 at gmail.com> wrote:
>>>> bm=dict.fromkeys(l,['-1','-1'])

When you call dict.fromkeys, it uses the same object as the key every
time. If you don't want that, try a dict comprehension instead:

bm = {x: ['-1', '-1'] for x in l}

This will construct a new list for every key, giving you the behaviour
you expect.

ChrisA



More information about the Python-list mailing list