My attempt to programmatically create a dictionary of lists does not allow me to individually access the dictionary keys. Whenever I create a dictionary of lists and try to add one key, they are all updated. Here is a very simple test case:
data = {} data = data.fromkeys(range(2),[]) data[1].append('hello') print data
Actual result: {0: ['hello'], 1: ['hello']}
Expected Result: {0: [], 1: ['hello']}
Works here
data = {0:[],1:[]} data[1].append('hello') print data
Actual and expected result: {0: [], 1: ['hello']}
Why fromkeys method fromkeys work as expected?
python dictionary list
Martin Burch Jul 16 2018-12-17T00: 00Z
source share