首页 > 解决方案 > 合并两个将所有参数从 list2 插入到 list1 的列表

问题描述

假设我有两个对象列表

List1 = [{"name" : "Mac", "age":24, "id" : 1},
         {"name" : "Mona","age":22, "id" : 2}]

List2 = [{"type" : "human","country":"AUS"}]

如何将列表 2 中的所有元素附加到列表 1 的所有元素中,以便最终的 list1 看起来像

[{"name" : "Mac", "age":24, "id" : 1, "type" : "human","country":"AUS"},{"name" : "Mona", "age":22, "id" : 2, "type" : "human","country":"AUS"}]

目前正在循环并在列表上进行更新,但我想知道是否有更简单更好的方法来做到这一点

for person in List1:    
  person.update(List2) 
print List1

标签: python-2.7

解决方案


List1 = [{"name" : "Mac", "age":24, "id" : 1},
     {"name" : "Mona","age":22, "id" : 2}]

List2 = [{"type" : "human","country":"AUS"}]

List1 = List1 + List2

这会起作用,我已经检查了 Python 2.7。希望这个答案有帮助。


推荐阅读