Add an element in each dictionary of a list (list comprehension)
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'}]
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"})
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