title: What is the Pythonic way to merge 2 lists of tuples and add the nonunique values in the tuples?
body: <p>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!</p>
  
  <pre><code>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()) &amp; 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
  </code></pre>