0

What is the best way to merge two lists into one and also combine double values? For example:

list_01 = [['2020-01-02', '2020-01-03', '2020-01-04', '2020-01-06'], 
           ['10', '20', '30', '40']]

list_02 = [['2020-01-04', '2020-01-05', '2020-01-06', '2020-01-07'],
           ['10', '20', '30', '40']]

The final list should look like this:

list_03 = [['2020-01-02', '2020-01-03', '2020-01-04', '2020-01-05', '2020-01-06', '2020-01-07'],
           ['10', '20', '40', '30', '70', '40']]

Whenever the dates have matched, the integer-values in the second column have been summed together.

Right now, my only real solution is to pass both lists trough several loops, but I wonder if there might be a better solution.

Thanks and a great evening for all of you.

1
  • According to your logic, shouldn't the fourth value of the new list be 20? As it is the value associated with 2020-01-05 in list_02
    – Tomerikoo
    Jan 24, 2020 at 21:05

3 Answers 3

3

Your "integers" should really be ints, not strings, and your lists should probably be Counters, as you seem to be counting things per day. Then you can simply add them:

from collections import Counter

list_01 = [['2020-01-02', '2020-01-03', '2020-01-04', '2020-01-06'], 
           ['10', '20', '30', '40']]

list_02 = [['2020-01-04', '2020-01-05', '2020-01-06', '2020-01-07'],
           ['10', '20', '30', '40']]

def to_counter(lst):
    return Counter(dict(zip(lst[0], map(int, lst[1]))))

counter = to_counter(list_01) + to_counter(list_02)

for item in counter.items():
    print(item)

Prints:

('2020-01-02', 10)
('2020-01-03', 20)
('2020-01-04', 40)
('2020-01-06', 70)
('2020-01-05', 20)
('2020-01-07', 40)
2
  • Thank you for your effort! This also solved my problem!
    – finethen
    Jan 24, 2020 at 21:22
  • + Good example of Counter usage.
    – funnydman
    Jan 25, 2020 at 7:19
2

Try this, make dictionaries. Let me know if this isn't what you want or is confusing.

dict_01 = {list_01[0][i]:int(list_01[1][i]) for i in range(len(list_01[0]))}
dict_02 = {list_02[0][i]:int(list_02[1][i]) for i in range(len(list_02[0]))}
dates = list(set(list_01[0] + list_02[0]))
dates.sort()
list_03 = [dates, [dict_01.get(date, 0) + dict_02.get(date, 0) for date in dates]]

@Tomerikoo points out a more elegant way to form the dictionaries.

dict_01 = dict(zip(*list_01))
dict_02 = dict(zip(*list_02))

As @HeapOverflow points out if you do this you should change the sums.

list_03 = [dates, [int(dict_01.get(date, 0)) + int(dict_02.get(date, 0)) for date in dates]]

This returns

[['2020-01-02', '2020-01-03', '2020-01-04', '2020-01-05', '2020-01-06', '2020-01-07'], [10, 20, 40, 20, 70, 40]]

I think this is right, and the 2020-01-05 should be 20 not 30.

8
  • 2
    A more elegant (and readable) way for creating the dicts would be: d_1 = dict(zip(*list_01)). Also I think you meant the 4th element should be 20, I agree with that
    – Tomerikoo
    Jan 24, 2020 at 21:02
  • @Tomerikoo Yes, except I think there is something a bit wrong with what you have written, at least when I run it it results in an error. Lemme see if I can fix it and add it to the answer. I meant the date is the 5th, but yes.
    – cmxu
    Jan 24, 2020 at 21:05
  • 1
    @Tomerikoo That wouldn't turn the strings into ints, though, so that would have to be done later. Jan 24, 2020 at 21:07
  • 1
    I think the answers already a bit cluttered lol, I'm just gonna leave it, but this was a productive comment string. Thanks guys.
    – cmxu
    Jan 24, 2020 at 21:13
  • 1
    Thank you for all the great help! This really solved my problem! And yes, you're all right: the 4th element should be 20!
    – finethen
    Jan 24, 2020 at 21:21
2

The best way to do it would probably be to use defaultdict, which provides a default value to each key, even if you've never introduced that key to the dictionary before. Then all you have to do is add whatever value belongs to that key (which is the date) from both lists. Then when you have this dictionary, just get the key-value pairs as items and unzip it into two lists.

from collections import defaultdict
mydict = defaultdict(int) # default values are 0

list_01 = [['2020-01-02', '2020-01-03', '2020-01-04', '2020-01-06'], ['10', '20', '30', '40']]
list_02 = [['2020-01-04', '2020-01-05', '2020-01-06', '2020-01-07'], ['10', '20', '30', '40']]

for t in [list_01, list_02]:
    for key, value in zip(t[0], t[1]):
        mydict[key] += int(value)

print(list(zip(*sorted(mydict.items()))))

This prints:

[('2020-01-02', '2020-01-03', '2020-01-04', '2020-01-05', '2020-01-06','2020-01-07'),
     (10, 20, 40, 20, 70, 40)]
1
  • Thank you for your effort! This also solved my problem!
    – finethen
    Jan 24, 2020 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.