Append List Inside Dictionary With Update
if i have below dictionary with one of the element is list, as follow: myDict = dict(a=1, b='2', c=[]) how do I update myDict and at the same time append c e.g. myDict .update(a=
Solution 1:
You can't use append
within update
because append
is trying to perform an inplace operation on the dict value. Try list concatenation instead:
d = dict(a=1, b='2', c=[])
d.update(a='one', b=2, c=d['c'] + ['newValue'])
print(d)
{'a': 'one', 'b': 2, 'c': ['newValue']}
Or:
d.update(a='one', b=2, c=d['c'] + ['newValue'] + ['anotherValue'])
Post a Comment for "Append List Inside Dictionary With Update"