Python – Odd elements indices
Last Updated :
19 Apr, 2023
Sometimes, while working with Python lists, we can have a problem in which we wish to find Odd elements. This task can occur in many domains such as web development and while working with Databases. We might sometimes, require to just find the indices of them. Let’s discuss certain way to find indices of Odd elements.
Method #1: Using loop This is brute force method in which this task can be performed. In this, we check for odd element in list and append its index accordingly.
Python3
test_list = [ 5 , 6 , 10 , 4 , 7 , 1 , 19 ]
print ("The original list is : " + str (test_list))
res = []
for idx, ele in enumerate (test_list):
if ele % 2 ! = 0 :
res.append(idx)
print ("Indices list Odd elements is : " + str (res))
|
Output :
The original list is : [5, 6, 10, 4, 7, 1, 19]
Indices list Odd elements is : [0, 4, 5, 6]
Time complexity: O(n), where n is the number of elements in the list.
Auxiliary space: O(m), where m is the number of odd elements in the list.
Method #2: Using list comprehension This is the shorthand by which this task can be performed. This method works in similar way as the above method. The difference is that it’s a one liner.
Python3
test_list = [ 5 , 6 , 10 , 4 , 7 , 1 , 19 ]
print ("The original list is : " + str (test_list))
res = [idx for idx, ele in enumerate (test_list) if ele % 2 ! = 0 ]
print ("Indices list Odd elements is : " + str (res))
|
Output :
The original list is : [5, 6, 10, 4, 7, 1, 19]
Indices list Odd elements is : [0, 4, 5, 6]
Time complexity: O(n), where n is the length of the test_list. The list comprehension takes O(n) time
Auxiliary Space: O(n), extra space of size n is required
Method #3: Here is another approach using filter() and lambda functions
enumerate(test_list) returns a list of tuples (index, element) for each element in test_list.
filter(lambda x: test_list[x[0]] % 2 != 0, enumerate(test_list)) filters out only those tuples x where test_list[x[0]] % 2 != 0, i.e., the element at the index is an odd number.
Finally, list() converts the filtered result into a list.
Python3
test_list = [ 5 , 6 , 10 , 4 , 7 , 1 , 19 ]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda x: test_list[x[ 0 ]] % 2 ! = 0 , enumerate (test_list)))
print ( "Indices list Odd elements is : " , [i[ 0 ] for i in res])
|
Output
The original list is : [5, 6, 10, 4, 7, 1, 19]
Indices list Odd elements is : [0, 4, 5, 6]
Time complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using while loop:
Python3
test_list = [ 5 , 6 , 10 , 4 , 7 , 1 , 19 ]
print ( "The original list is : " + str (test_list))
res = []
i = 0
while i < len (test_list):
if test_list[i] % 2 ! = 0 :
res.append(i)
i + = 1
print ( "Indices list of odd elements is : " + str (res))
|
Output
The original list is : [5, 6, 10, 4, 7, 1, 19]
Indices list of odd elements is : [0, 4, 5, 6]
Time complexity: O(n)
Auxiliary Space: O(n)
Method 5 : Using numpy module
Here is another approach using the numpy module. We can use numpy’s boolean indexing to create a boolean array of True for odd elements and False for even elements. We can then use numpy’s where function to get the indices of the True elements, which will give us the indices of the odd elements.
- Import the numpy module using the import statement.
- Initialize the list test_list with some values.
- Print the original list using the print() statement.
- Create a numpy array from the list using the np.array() function.
- Use the % operator to create a boolean array of True for odd elements and False for even elements. Store the boolean array in bool_arr.
- Use the np.where() function to get the indices of the True elements in bool_arr. The np.where() function returns a tuple with one element, which is an array of the indices of the True elements. Since we are only interested in the indices, we use [0] to get the array of indices.
- Store the array of indices in the variable res.
- Print the result using the print() statement.
Python3
import numpy as np
test_list = [ 5 , 6 , 10 , 4 , 7 , 1 , 19 ]
print ( "The original list is : " + str (test_list))
arr = np.array(test_list)
bool_arr = arr % 2 ! = 0
res = np.where(bool_arr)[ 0 ]
print ( "Indices list Odd elements is : " , res)
|
OUTPUT:
The original list is : [5, 6, 10, 4, 7, 1, 19]
Indices list Odd elements is : [0 4 5 6]
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(n), as we are creating a numpy array of the same length as the list.
Method 6: Using itertools library
- Import the itertools library.
- Use the compress function to create a list of odd elements.
- Use the count function to create a list of indices.
- Use the compress function again to create a list of odd element indices.
- Print the list of odd element indices.
Python3
import itertools
test_list = [ 5 , 6 , 10 , 4 , 7 , 1 , 19 ]
print ( "The original list is : " + str (test_list))
odd_elements = list (itertools.compress(test_list, [x % 2 ! = 0 for x in test_list]))
indices = itertools.count()
odd_indices = [i for i, x in zip (indices, [x % 2 ! = 0 for x in test_list]) if x]
print ( "Indices list Odd elements is : " , odd_indices)
|
Output
The original list is : [5, 6, 10, 4, 7, 1, 19]
Indices list Odd elements is : [0, 4, 5, 6]
Time complexity: The time complexity of this method is O(n), where n is the length of the input list test_list.
Auxiliary space: The auxiliary space used by this method is O(n), where n is the length of the input list test_list.
Method 7: Using heapq:
Algorithm:
- Initialize a list “test_list” with some elements.
- Print the original list “test_list”.
- Use list comprehension to create a new list “res” which contains the indices of all odd elements in “test_list”.
- Print the list “res”.
Python3
import heapq
test_list = [ 5 , 6 , 10 , 4 , 7 , 1 , 19 ]
print ( "The original list is : " + str (test_list))
res = [i for i, elem in enumerate (test_list) if elem % 2 ! = 0 ]
print ( "Indices list of odd elements is : " , res)
|
Output
The original list is : [5, 6, 10, 4, 7, 1, 19]
Indices list of odd elements is : [0, 4, 5, 6]
Time Complexity:
The time complexity of this code is O(n) where n is the length of the input list “test_list”. This is because we need to iterate over the list once to find the odd elements.
Space Complexity:
The space complexity of this code is O(k) where k is the number of odd elements in the input list “test_list”. This is because we create a new list “res” which contains the indices of all odd elements in “test_list”. The space required for this list depends on the number of odd elements.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...