91

I have a list of dictionaries, and want to add a key for each element of this list. I tried:

result = [ item.update({"elem":"value"}) for item in mylist ]

but the update method returns None, so my result list is full of None.

result = [ item["elem"]="value" for item in mylist ]

returns a syntax error.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
MickaëlG
  • 1,377
  • 1
  • 11
  • 17
  • 1
    Just for the sake of clarification, even though the first approach updates the list referred as `my_list`, which is the desired effect, the `result` is a list of `None` because this is the result of the `item.update` execution on each item list, this function itself returns nothing, [as stated here](https://www.tutorialspoint.com/python/dictionary_update.htm). – dandev486 Dec 18 '18 at 11:45

7 Answers7

101

If you want to use list comprehension, there's a great answer here: https://stackoverflow.com/a/3197365/4403872

In your case, it would be like this:

result = [dict(item, **{'elem':'value'}) for item in myList]

Eg.:

myList = [{'a': 'A'}, {'b': 'B'}, {'c': 'C', 'cc': 'CC'}]

Then use either

result = [dict(item, **{'elem':'value'}) for item in myList]

or

result = [dict(item, elem='value') for item in myList]

Finally,

>>> result
[{'a': 'A', 'elem': 'value'},
 {'b': 'B', 'elem': 'value'},
 {'c': 'C', 'cc': 'CC', 'elem': 'value'}]
Community
  • 1
  • 1
vk1011
  • 7,011
  • 6
  • 26
  • 42
69

You don't need to worry about constructing a new list of dictionaries, since the references to your updated dictionaries are the same as the references to your old dictionaries:

 for item in mylist:
    item.update( {"elem":"value"})
Jeffrey Theobald
  • 2,469
  • 18
  • 15
19

traditionally but faster than the other (sophisticated) answers:

myList = [{'a': 'A'}, {'b': 'B'}, {'c': 'C', 'cc': 'CC'}]
for item in myList: item['elem']='value'

some timings:

%timeit result = [dict(item, **{'elem':'value'}) for item in myList]
865 ns ± 8.89 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%timeit result = [dict(item, elem='value') for item in myList]
854 ns ± 5.37 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%timeit result = list(map(lambda item: dict(item, elem='value'), myList))
1.22 µs ± 86.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%timeit for item in myList: item.update({'elem':'value'})
464 ns ± 3.72 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%timeit for item in myList: item['elem']='value'
124 ns ± 0.31 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

%timeit [dict(list(item.items()) + [("elem", "value")]) for item in myList]
1.59 µs ± 14.8 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
eid
  • 537
  • 5
  • 12
  • 1
    Thanks! This defies my intuition. On longer lists of maps this also holds true: ```python myList = [{'a': 'A'}, {'b': 'B'}, {'c': 'C', 'cc': 'CC'}] myList = myList * 25000 %timeit result = [dict(item, **{'elem':'value'}) for item in myList] 14.4 ms ± 219 µs per loop %timeit for item in myList: item.update({'elem':'value'}) 5.44 ms ± 151 µs per loop %timeit for item in myList: item['elem']='value' 1.51 ms ± 52 µs per loop %timeit [dict(list(item.items()) + [("elem", "value")]) for item in myList] 24.2 ms ± 564 µs per loop ``` – russellthehippo Dec 12 '22 at 21:15
17

@vk1011's answer is good and can be extended with the spread operator concisely and new dictionary objects are an added benefit

  1. To override any existing key's value with the new one you can put the spread operator before the new item

    result = [{**item, 'elem':'value'} for item in myList]
    
  2. To override the new entry's value with an existing one, use the spread operator after the new item

    result = [{'elem':'value', **item} for item in myList]
    

Both methods will give a list of dictionary objects including the new item

14

You can use map.

result = map(lambda item: dict(item, elem='value'), myList)

If you already have the list of lements you can do:

#elements = ['value'] * len(myList)
result = map(lambda item: dict(item[0], elem=item[1]),zip(myList,elements))

then you have the results

Asive Dlaba
  • 161
  • 1
  • 4
5
>>> a = [ { 1:1 }, {2:2}, {3:3} ]
>>> for item in a:
...     item.update( { "test": "test" } )
... 
>>> a
[{'test': 'test', 1: 1}, {'test': 'test', 2: 2}, {'test': 'test', 3: 3}]

You are using a list comprehension incorrectly, the call to item.update returns a None value and thus your newly created list will be full of None values instead of your expected dict values.

You need only to loop over the items in the list and update each accordingly, because the list holds references to the dict values.

sean
  • 3,955
  • 21
  • 28
2

Either do not use a list comprehension, or return a new dict based on the original dict plus the new key:

[dict(list(item.items()) + [("elem", "value")]) for item in mylist]
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343