0

I have list of dictionaries like:

>>> lst = [
...     {'a': 1, 'b': 2},
...     {'a': 3, 'b': 4}
... ]
>>> [x for x in lst]
[{'a': 1, 'b': 2}, {'a': 3, 'b': 4}]

and I'd like to add new item to each dictionary:

>>> [x.update({'c': '0'}) for x in lst]
[None, None]

So how to do this?

vedar
  • 483
  • 8
  • 15
  • Ok, thanks, I took the answer from http://stackoverflow.com/questions/3197342/how-to-use-list-comprehension-to-add-an-element-to-copies-of-a-dictionary/3197365#3197365: `[dict(x, **{'c': 0}) for x in lst]`, but just wow..., isn't there a nicer way? – vedar Jan 05 '17 at 17:40
  • 1
    `{'c': '0', **x}` in Python 3.5 – vaultah Jan 05 '17 at 17:42
  • Personally limited to 2.7... – vedar Jan 05 '17 at 17:43

0 Answers0