Updating a list of python dictionaries with a key, value pair from another list

前端 未结 4 2016
青春惊慌失措
青春惊慌失措 2021-02-19 19:35

Let\'s say I have the following list of python dictionary:

dict1 = [{\'domain\':\'Ratios\'},{\'domain\':\'Geometry\'}]

and a list like:

4条回答
  •  孤街浪徒
    2021-02-19 20:04

    Using list comprehension will be the pythonic way to do it.

    [data.update({'count': list1[index]}) for index, data in enumerate(dict1)]
    

    The dict1 will be updated with the corresponding value from list1.

提交回复
热议问题