Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | List consisting of all the alternate elements

Python | List consisting of all the alternate elements

Some of the list operations are quite general and having shorthands without needing to formulate a multiline code is always required. Wanting to construct a list consisting of all the alternate elements of the original list is a problem that one developer faces in day-day applications. Let’s discuss certain ways to print all the alternate elements of the given list. 

Method #1 : Using list comprehension Shorthand to the naive method, list comprehension provides a faster way to perform this particular task. In this method, all the indices which are not multiple of 2, hence odd are inserted in the new list. 

Python3




# Python code to demonstrate
# to construct alternate element list
# using list comprehension
 
# initializing list
test_list = [1, 4, 6, 7, 9, 3, 5]
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension
# to construct alternate element list
res = [test_list[i] for i in range(len(test_list)) if i % 2 != 0]
 
# printing result
print("The alternate element list is : " + str(res))


Output

The original list : [1, 4, 6, 7, 9, 3, 5]
The alternate element list is : [4, 7, 3]

Time complexity: O(n), where n is the number of elements in the list.
Auxiliary space: O(n), as we are constructing a new list with the same number of elements as the original list

Method #2 : Using enumerate() This is just a variation to the list comprehension method but does the similar internal working as list comprehension but uses different variables to keep track of index along with its value. 

Python3




# Python code to demonstrate
# to construct alternate element list
# using enumerate()
 
# initializing list
test_list = [1, 4, 6, 7, 9, 3, 5]
 
# printing original list
print("The original list : " + str(test_list))
 
# using enumerate()
# to construct alternate element list
res = [i for j, i in enumerate(test_list) if j % 2 != 0]
 
# printing result
print("The alternate element list is : " + str(res))


Output

The original list : [1, 4, 6, 7, 9, 3, 5]
The alternate element list is : [4, 7, 3]

Time Complexity: O(n), where n is the length of the input list. This is because we’re using enumerate() which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), as we’re using additional space res other than the input list itself with the same size of input list.

Method #3 : Using Slice notation This the most pythonic and elegant way to perform this particular task and enhances to the full power of python. We can use the skip utility provided by slice to get the alternate elements from start to end in the list. 

Python3




# Python code to demonstrate
# to construct alternate element list
# using Slice notation
 
# initializing list
test_list = [1, 4, 6, 7, 9, 3, 5]
 
# printing original list
print("The original list : " + str(test_list))
 
# using Slice notation
# to construct alternate element list
res = test_list[1::2]
 
# printing result
print("The alternate element list is : " + str(res))


Output

The original list : [1, 4, 6, 7, 9, 3, 5]
The alternate element list is : [4, 7, 3]

The time complexity of this code is O(n), where n is the length of the input list.

The auxiliary space complexity of this code is O(k), where k is the length of the resulting alternate element list. 

Method #4 : Using for loop

Python3




# Python code to demonstrate
# to construct alternate element list
 
# initializing list
test_list = [1, 4, 6, 7, 9, 3, 5]
 
# printing original list
print("The original list : " + str(test_list))
res = []
for i in range(1, len(test_list), 2):
    res.append(test_list[i])
 
# printing result
print("The alternate element list is : " + str(res))


Output

The original list : [1, 4, 6, 7, 9, 3, 5]
The alternate element list is : [4, 7, 3]

Time complexity: O(n), where n is the length of the input list. This is because the for loop runs for half the length of the input list, i.e. n/2.

Auxiliary space complexity: O(n/2) = O(n), since the output list ‘res’ has a length of n/2 at most.

Method #5 : Using itertools

One other approach that you can use to get the alternate elements from a list is to use the itertools module. The itertools.islice() function allows you to slice an iterator by specifying a start, stop, and step. You can use this function to get the alternate elements from a list by specifying the start index as 1, the step as 2, and the stop index as the length of the list.

Here’s an example of how you can use itertools.islice() to get the alternate elements from a list:

Python3




import itertools
 
# Initialize the list
test_list = [1, 4, 6, 7, 9, 3, 5]
 
# Get the alternate elements using itertools.islice()
res = itertools.islice(test_list, 1, len(test_list), 2)
 
# Print the result
print(list(res))
 
#This code is contributed by Edula Vinay Kumar Reddy


Output

[4, 7, 3]

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

Method #6 : Using  map and lambda function:

Python3




# initializing list
test_list = [1, 4, 6, 7, 9, 3, 5]
# printing original list
print("The original list : " + str(test_list))
# using list comprehension to construct alternate element list
# iterating over the list and checking if index is odd or not
# if index is odd, adding the element to the result list
res = [test_list[i] for i in range(len(test_list)) if i % 2 != 0]
# printing result
print("The alternate element list is : " + str(res))
#This code is contributed by Jyothi pinjala.


Output

The original list : [1, 4, 6, 7, 9, 3, 5]
The alternate element list is : [4, 7, 3]

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

Method#7: Using numpy

Python3




# importing numpy
import numpy as np
 
# initializing list
test_list = [1, 4, 6, 7, 9, 3, 5]
 
# printing original list
print("The original list : " + str(test_list))
 
# using numpy
res = np.array(test_list)[1::2].tolist()
 
# printing result
print("The alternate element list is : " + str(res))
#This code is contributed by Vinay Pinjala.


Output

The original list : [1, 4, 6, 7, 9, 3, 5]
The alternate element list is : [4, 7, 3]

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

Method#8: Using heapq:

Algorithm :

  1. Initialize the list “test_list” with some elements.
  2. Print the original list “test_list”.
  3. Using heapq and list comprehension to construct the alternate element heap:
    a. Create an empty list “res”.
    b. Iterate over the “test_list” using enumerate() function to get the index and value.
    c. Check if the index is odd or not.
    d. If the index is odd, add the value to the result list “res”.
    e. Apply the heapq.heapify() method to the result list “res” to convert it into a heap.
  4. Print the alternate element heap “res”.
     

Python3




import heapq
 
# initializing list
test_list = [1, 4, 6, 7, 9, 3, 5]
# printing original list
print("The original list : " + str(test_list))
# using heapq to construct alternate element list
# iterating over the list and checking if index is odd or not
# if index is odd, adding the element to the result list
res = [val for idx, val in enumerate(test_list) if idx % 2 != 0]
heapq.heapify(res)
# printing result
print("The alternate element heap is : " + str(res))
#This code is contributed by Rayudu


Output

The original list : [1, 4, 6, 7, 9, 3, 5]
The alternate element heap is : [3, 7, 4]

Time Complexity: O(n log n), where n is the number of elements in the list. The reason for this is the time complexity of the heapq.heapify() method, which takes O(n log n) time.

Space Complexity:  O(n), where n is the number of elements in the list. The reason for this is the space required to store the alternate element heap.

RELATED ARTICLES

Most Popular

Recent Comments