How do i add two lists' elements into one list?
A solution using a loop that you try is one way, this is more beginner friendly than Xions solution.
list3 = []
for index, item in enumerate(list1):
list3.append(list1[index] + list2[index])
This will also work for a shorter solution. Using map() and lambda, I prefer this over zip, but thats up to everyone
list3 = map(lambda x, y: str(x) + str(y), list1, list2);
You can use list comprehensions with zip
:
list3 = [a + b for a, b in zip(list1, list2)]
zip
produces a list of tuples by combining elements from iterables you give it. So in your case, it will return pairs of elements from list1
and list2
, up to whichever is exhausted first.