Given n lists with m dictionaries as their elements, I would like to produce a new list, with a joined set of dictionaries. Each dictionary is guaranteed to have a key called “index”, but could have an arbitrary set of keys beyond that. The non-index keys will never overlap across lists. For example, imagine the following two lists:
l1 = [{"index":1, "b":2}, {"index":2, "b":3}, {"index":3, "green":"eggs"}]
l2 = [{"index":1, "c":4}, {"index":2, "c":5}]
("b" would never appear in l2, since it appeared in l1, and similarly, "c" would never appear in l1, since it appeared in l2)
I would like to produce a joined list:
l3 = [{"index":1, "b":2, "c":4},
{"index":2, "b":3, "c":5},
{"index":3, "green":"eggs"}]
What is the most efficient way to do this in Python?
EDIT: Since
l3is not guaranteed to be sorted (.values()returns items in no specific order), you can do as @user560833 suggests: