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.

Respuesta :

Answer:

list1 = [1, 2, 3]

list2 = [4, 5, 6]

list3 = []

newlist = []

for i in list1:

   for j in list2:

       if list1.index(i) == list2.index(j):

           newlist.append(j)

           newlist.append(i)

           break

   

for i in reversed(newlist):

   list3.append(i)

print(list3)

Explanation:

The programming language used is python.

List 1 and 2 are initialized, and two empty lists are initialized too, these two lists are going to be used in generating the new list.

Two FOR loops are used for both list one and two respectively, to iterate through their content, the IF statement and the break is placed within the for loop to ensure that there is no repetition.

The index of list 1 and list 2 are appended (added) to a new list one at a time.

The new list is then reversed and its content are added to list 3 to give the final solution.

NOTE: The reason a separate list was created was because the reversed() function does not return a list, so in order to get the list, it must be added to an empty list as you reverse it.

Ver imagen jehonor