1. Engineering
  2. Computer Science
  3. using python given the lists list1 and list2 not necessarily...

Question: using python given the lists list1 and list2 not necessarily...

Question details

USING PYTHON: Given the lists list1 and list2, not necessarily of the same length, create a new list consisting of alternating elements of list1 and list2 (that is, the first element of list1 followed by the first element of list2 , followed by the second element of list1, followed by the second element of list2, and so on. Once the end of either list is reached, the remaining elements of the longer list is added to the end of the new list. For example, if list1 contained [1, 2, 3] and list2 contained [4, 5, 6, 7, 8], then the new list should contain [1, 4, 2, 5, 3, 6, 7, 8]. Associate the new list with the variable list3.

My Solution that doesn't work: I do not need to print the function, I just need the basic code. We are using MyProgrammingLab.

list3 = []
range1 = len(list1)
range2 = len(list2)
if range1 < range2:
   full_range = range1
   else:
       full_range = range2
       for i in range(full_range):
           if i < range1:
               list3.append(list1[i])
               if i < range2:
                   list3.append(list2[i])

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution