dvas0004 / NerdNotes

A collection of notes: things I'd like to remember while reading technical articles, technical questions I couldn't answer, and so on.
11 stars 0 forks source link

Python: Using "map" to update a list of dictionary #111

Open dvas0004 opened 4 years ago

dvas0004 commented 4 years ago
old_list = [{
   'fieldToUpdate': 'oldValue'
}]

new_list = list( map( lambda entry: entry.update({'fieldToUpdate': 'newValue'}) or entry, old_list) )

# new_list is now: [{'fieldToUpdate': 'newValue'}]

The above code snippet makes use of the fact that the "update" method of a standard python dict returns "None" - hence you can see the "or" being used to returned the newly updated entry