Open In App

Python | Separate odd and even index elements

Improve
Improve
Like Article
Like
Save
Share
Report

Python list are quite popular and no matter what type of field one is coding, one has to deal with lists and its various applications. In this particular article, we discuss ways to separate odd and even indexed elements and its reconstruction join. Let’s discuss ways to achieve this. 

Method #1 : Using Naive method
Using Naive method, this task can be performed by using the loops. One can use two containers one each to store alternate elements and later joining them.

Python3




# Python3 code to demonstrate
# Separating odd and even index elements
# using naive method
 
# initializing list
test_list = [3, 6, 7, 8, 9, 2, 1, 5]
 
# printing original list
print("The original list : " + str(test_list))
 
# using naive method
# Separating odd and even index elements
odd_i = []
even_i = []
for i in range(0, len(test_list)):
    if i % 2:
        even_i.append(test_list[i])
    else :
        odd_i.append(test_list[i])
 
res = odd_i + even_i
 
# print result
print("Separated odd and even index list: " + str(res))


Output

The original list : [3, 6, 7, 8, 9, 2, 1, 5]
Separated odd and even index list: [3, 7, 9, 1, 6, 8, 2, 5]

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.  
Auxiliary Space: O(n), where n is the number of elements in the new res list 

 
Method #2 : Using list slicing 
This particular task can be easily performed using the list slicing method in a more compact and efficient manner, this is a recommended method to solve this problem.

Python3




# Python3 code to demonstrate
# Separating odd and even index elements
# Using list slicing
 
# initializing list
test_list = [3, 6, 7, 8, 9, 2, 1, 5]
 
# printing original list
print("The original list : " + str(test_list))
 
# Using list slicing
# Separating odd and even index elements
res = test_list[::2] + test_list[1::2]
 
# print result
print("Separated odd and even index list : " + str(res))


Output

The original list : [3, 6, 7, 8, 9, 2, 1, 5]
Separated odd and even index list : [3, 7, 9, 1, 6, 8, 2, 5]

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

Method #3 : Using the itertools.chain() function in combination with the itertools.islice() 

You can use the itertools.chain() function in combination with the itertools.islice() function to separate the odd and even index elements of a list such that the odd index elements come first and the even index elements come second.

The itertools.islice() function allows you to slice an iterator by index, and it can be used in place of list slicing to achieve the same result.

Here is an example of how to use itertools.chain() and itertools.islice() to separate the odd and even index elements of a list:

Python3




#Python3 code to demonstrate
#Separating odd and even index elements
#odd index elements first, even index elements second
#using itertools.chain() and itertools.islice()
import itertools
#initializing list
test_list = [3, 6, 7, 8, 9, 2, 1, 5]
 
#printing original list
print("The original list : " + str(test_list))
 
#Using itertools.chain() and itertools.islice()
#Separating odd and even index elements
odd_i = itertools.islice(test_list, 0, None, 2)
even_i = itertools.islice(test_list, 1, None, 2)
res = list(itertools.chain(odd_i, even_i))
 
#print result
print("Separated odd and even index list : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list : [3, 6, 7, 8, 9, 2, 1, 5]
Separated odd and even index list : [3, 7, 9, 1, 6, 8, 2, 5]

This method has a time complexity of O(n) and an auxiliary space of O(n), as it involves creating a new list to store the separated odd and even index elements.



Last Updated : 06 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads