Python | Update a list of tuples using another list
Given two list of tuples, write a Python program to update ‘list1’ according to the ‘list2’ and return an updated list.
Examples:
Input : list1 = [('x', 0), ('y', 5)] list2 = [('x', 100), ('y', 200)] Output : [('x', 100), ('y', 200)] Input : list1 = [('a', 0), ('b', 0), ('c', 0)] list2 = [('a', 1), ('b', 2)] Output :[('a', 1), ('b', 2), ('c', 0)]
Approach #1 Pythonic Naive
This is a Pythonic naive approach. We simply convert the list of tuples into a dictionary and just update it with list2 and convert the dictionary back to list.
# Python3 code to Update a list # of tuples according to another list def merge(list1, list2): dic = dict (list1) dic.update( dict (list2)) return list (dic.items()) # Driver Code list1 = [( 'a' , 0 ), ( 'b' , 0 ), ( 'c' , 0 )] list2 = [( 'a' , 5 ), ( 'c' , 3 )] print (merge(list1, list2)) |
[('a', 5), ('b', 0), ('c', 3)]
Approach #2 Using defaultdict
Python collections module provides defaultdict() method which is used in this approach. First we initialize ‘dic’ with defaultdict method passing list as factory. Use a loop to append the left element of each tuple as key and right element of each tuple as value for both the lists. Now simply use the sorted function and produce the list in such a way that the for each unique key it keeps the maximum value of right element of tuple.
# Python3 code to Update a list # of tuples according to another list from collections import defaultdict def merge(list1, list2): dic = defaultdict( list ) for i, j in list1 + list2: dic[i].append(j) return sorted ([(i, max (j)) for i, j in dic.items()], key = lambda x:x[ 0 ]) # Driver Code list1 = [( 'a' , 0 ), ( 'b' , 0 ), ( 'c' , 0 )] list2 = [( 'a' , 5 ), ( 'c' , 3 )] print (merge(list1, list2)) |
[('a', 5), ('b', 0), ('c', 3)]
Recommended Posts:
- Python | Convert list of tuples to list of list
- Python program to create a list of tuples from given list having number and its cube in each tuple
- Python | Convert list of strings to list of tuples
- Python | Convert list of tuples to list of strings
- Python | Count tuples occurrence in list of tuples
- Python | Remove duplicate tuples from list of tuples
- Python | Convert string tuples to list tuples
- Python | Find the tuples containing the given element from a list of tuples
- Python | Remove tuples from list of tuples if greater than n
- Python | Remove tuples having duplicate first value from given list of tuples
- Python | Convert list of tuples into list
- Python | Combining tuples in list of tuples
- Python | Min and Max value in list of tuples
- Python | Update each element in tuple list
- Python | Summation of tuples in list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.