Python | Separate odd and even index elements
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.
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]
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.
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]