-1

I am stuck on a problem involving adding two lists together. For example, if list1 was [1,2,3,4] and list2 was [2,4] I would have to return [3,5,3,4]. Or if list1=[0] and list2=[1] I would return [1]

def addsum(list1,list2):
    new_list = []
    list1[0]+list2[0] = new_list[0]

and so on. This was my first approach but I'm getting a lot of errors. I'm new to lists so I can't use index or lambda functions. I am only allowed to use len(). Would appreciate the help.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
Jessica
  • 139
  • 2
  • 9

1 Answers1

0

You may want to check out 2 python concepts: list comprehension (http://www.secnetix.de/olli/Python/list_comprehensions.hawk) and condensed form of if-else (https://stackoverflow.com/a/2802748/1045285).

l1 = [1, 2, 4, 5]
l2 = [2, 4]
min_len = min(len(l1), len(l2))

rem = l1[min_len:] if len(l1) > len(l2) else l2[min_len:]
res = [e1 + e2 for e1, e2 in zip(l1[:min_len], l2[:min_len])] + rem
Community
  • 1
  • 1
Chintak
  • 365
  • 4
  • 14