Li Xiong
Li Xiong

Reputation: 738

What is the Pythonic way to merge 2 lists of tuples and add the nonunique values in the tuples?

I am looking for the best way to refactor the Python code below. I think there is a Pythonic way to do this in 2 or 3 lines of code, but couldn't figure it out. I have searched Stackoverflow but couldn't find similar problems and solutions. Many thanks!

list1 = [(Python, 5), (Ruby, 10), (Java, 15), (C++, 20)]
list2 = [(Python, 1), (Ruby, 2), (Java, 3), (PHP, 4), (Javascript, 5)]

# I want to make an unsorted list3 like this
# list3 = [(Python, 6), (Ruby, 12), (Java, 18), (PHP, 4), (Javasript, 5), (C++, 20)]
common_keys = list(set(dict(list1).keys()) & set(dict(list2).keys()))

if common_keys:
    common_lst = [(x, (dict(list1)[x] + dict(list2)[x])) for x in common_keys]
    rest_list1 = [(x, dict(list1)[x]) for x in dict(list1).keys() if x not in common_keys]
    rest_list2 = [(x, dict(list2)[x]) for x in dict(list2).keys() if x not in common_keys]
    list3 = common_lst + rest_list1 + rest_list2

else:
    list3 = list1 + list2

Upvotes: 1

Views: 1278

Answers (1)

agf
agf

Reputation: 176910

You're looking for collections.defaultdict:

from collections import defaultdict
from itertools import chain

merged = defaultdict(int)

for key, value in chain(list1, list2):
    merged[key] += value

If you want a list of tuples:

list3 = merged.items()

If you want to do it without chain, you can do it as:

from collections import defaultdict

merged = defaultdict(int)

merged.update(list1)

for key, value in list2:
    merged[key] += value

Edit: As Beni points out in a comment, on 2.7/3.2+, you can do:

from collections import Counter

merged = Counter(dict(list1))
merged.update(dict(list2))

Which requires you convert the lists to dicts but is otherwise perfect.

Upvotes: 5

Related Questions