Open In App

Python – Alternate List elements

Last Updated : 14 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given 2 lists, print the element in zig-zag manner, i.e print similar indices of lists and then proceed to next.

Input : test_list1 = [5, 3, 1], test_list2 = [6, 4, 2] 
Output : [5, 6, 3, 4, 1, 2, 4] 
Explanation : 5 and 6, as in 0th index are printed first, then 3 and 4 on 1st index, and so on. 

Input : test_list1 = [5, 3, 1, 9], test_list2 = [6, 4, 2, 10] 
Output : [5, 6, 3, 4, 1, 2, 4, 9, 10] 
Explanation : 5 and 6, as in 0th index are printed first, then 3 and 4 on 1st index, and so on.

Method #1 : Using loop  

This is one of the ways in which this task can be performed. In this we plainly perform iteration and append the similar index elements one after another in result list.

Python3




# Python3 code to demonstrate working of
# Alternate List elements
# Using loop
 
# initializing lists
test_list1 = [5, 3, 1, 4, 7]
test_list2 = [6, 4, 2, 5, 1]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Using loop to print elements in criss cross manner
res = []
for idx in range(0, len(test_list1)):
    res.append(test_list1[idx])
    res.append(test_list2[idx])
 
# printing result
print("The zig-zag printing of elements : " + str(res))


Output

The original list 1 : [5, 3, 1, 4, 7]
The original list 2 : [6, 4, 2, 5, 1]
The zig-zag printing of elements : [5, 6, 3, 4, 1, 2, 4, 5, 7, 1]

Time complexity: O(n)
Auxiliary Space: O(n)

Method #2 : Using zip() + loop

The combination of above functions can be used to solve this problem. In this, we pair each element with similar index using zip() and then each element is fed into result list using loop.

Python3




# Python3 code to demonstrate working of
# Alternate List elements
# Using  zip() + loop
 
# initializing lists
test_list1 = [5, 3, 1, 4, 7]
test_list2 = [6, 4, 2, 5, 1]
 
# printing original lists
print("The original list 1 : " + str(test_list1))
print("The original list 2 : " + str(test_list2))
 
# Using zip() to perform pairing and loop to
# get elements into result list
res = []
for ele1, ele2 in zip(test_list1, test_list2):
    res.append(ele1)
    res.append(ele2)
     
# printing result
print("The zig-zag printing of elements : " + str(res))


Output

The original list 1 : [5, 3, 1, 4, 7]
The original list 2 : [6, 4, 2, 5, 1]
The zig-zag printing of elements : [5, 6, 3, 4, 1, 2, 4, 5, 7, 1]

Time complexity: O(n*n), where n is the length of the test_list. The zip() + loop takes O(n*n) time
Auxiliary Space: O(n), extra space of size n is required

Method #3 : Using itertools module 

step-by-step implementation of algorithm 

  • Import the itertools module
  • Initialize two lists test_list1 and test_list2
  • Using zip function to combine the elements of the list’s element by element and create a list of tuples. And store them in combined_list 
  • Use the itertools.chain function to concatenate the tuples in the combined_list into   a single list, we used “*” operator unpacks the list of tuples into separate arguments.
  • Store the concatenated list in the variable result
  • Print the result list.

Python3




import itertools
 
# Test lists to combine
test_list1 = [5, 3, 1, 4, 7]
test_list2 = [6, 4, 2, 5, 1]
 
# Use the zip function to combine the elements of the two lists
# element-wise and create a list of tuples
combined_list = list(zip(test_list1, test_list2))
 
# Use the itertools.chain function to concatenate the tuples
# in the combined_list into a single list
result = list(itertools.chain(*combined_list))
 
# Print the result
print(result)


Output

[5, 6, 3, 4, 1, 2, 4, 5, 7, 1]

Time complexity: O(n)

Auxiliary space: O(n)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads