I need to merge 2 lists, but sum each item in these lists. It would be this here:
list1 = [1,3,9,7]
list2 = [1,2,3,4,5]
//what I need: [list1(item1) + list2(item1), list1(item2) + list2(item2)...]
expected_return: = [2,5,12,11,5]
I managed to just combine the lists
3
Answers
You can loop the the items and create a new list. you need to make sure the index isnot out of range.
If you want a more functional approach, you can first create a helper function that adds dummy elements so that the two lists are of equal lengths, use
IterableZip
frompackage:collection
to combine the two lists into a list of pairs, and then callreduce
on each pair:To sum corresponding elements from two lists in Flutter, you can use List.generate() along with max() to handle lists of different lengths. Here’s the code snippet: