Issue
listA = [['a','1'],['e','5'],['i','3'],['o','7'],['u','3']]
listB = [['a','6'],['b','3'],['c','4'],['d','1'],['e','2']]
Now, these lists represent item count. And i want to merge these two lists so that i can see the count of each item in the other list as well.
If an item is present in one list and is not present in the other list, then i want to state that as '0' rather than null. As shown below:
output = [['a','1','6'],['e','5','2'],['i','3','0'],['o','7','0'],
['u','3','0'],['b','0','3'],['c','0','4'],['d','0','1']]
Edit: Just to clarify the question for future reference. It has already been answered.
Solution
listA = [['a','1'],['e','5'],['i','3'],['o','7'],['u','3']]
listB = [['a','6'],['b','3'],['c','4'],['d','1'],['e','2']]
dictA = dict(listA)
dictB = dict(listB)
result = []
for key, value in listA:
result.append([key, value, dictB.get(key, '0')])
for key, value in listB:
if key not in dictA:
result.append([key, '0', value])
print(result)
# [['a', '1', '6'], ['e', '5', '2'], ['i', '3', '0'], ['o', '7', '0'], ['u', '3', '0'], ['b', '0', '3'], ['c', '0', '4'], ['d', '0', '1']]
Answered By - bug
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.