Python – Creating new list with values from two prior lists

listpython

Given the lists list1 and list2 that are of the same length, create a new list consisting of the last element of list1 followed by the last element of list2 , followed by the second to last element of list1 , followed by the second to last element of list2 , and so on (in other words the new list should consist of alternating elements of the reverse of list1 and list2 ). For example, if list1 contained [1, 2, 3] and list2 contained [4, 5, 6] , then the new list should contain [3, 6, 2, 5, 1, 4] . Associate the new list with the variable list3 .

My code:

def new(list1,list2):
    i = 0
    j = 0
    new_list = []
    for j in list1:
        new_list[i-1] = list2[j-1]
        i+= 1
        j += 1
        new_list[i-1] = list2 [j-1]
        i+= 1
        j += 1
    return new_list

I know, it's messy =_=, help?

Best Answer

l1 = [1,2,3]
l2 = [4,5,6]

newl = []
for item1, item2 in zip(reversed(l1), reversed(l2)):
    newl.append(item1)
    newl.append(item2)

print newl