Saturday, December 28, 2024
Google search engine
HomeLanguagesPython – Odd elements indices

Python – Odd elements indices

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




# Python3 code to demonstrate working of
# Odd elements indices
# using loop
 
# initialize list
test_list = [5, 6, 10, 4, 7, 1, 19]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Odd elements indices
# using loop
res = []
for idx, ele in enumerate(test_list):
    if ele % 2 != 0:
        res.append(idx)
         
# printing result
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




# Python3 code to demonstrate working of
# Odd elements indices
# using list comprehension
 
# initialize list
test_list = [5, 6, 10, 4, 7, 1, 19]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Odd elements indices
# using list comprehension
res = [idx for idx, ele in enumerate(test_list) if ele % 2 != 0]
         
# printing result
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




# Python3 code to demonstrate working of
# Odd elements indices
# using filter and enumerate
   
# initialize list
test_list = [5, 6, 10, 4, 7, 1, 19]
   
# printing original list
print("The original list is : " + str(test_list))
   
# Odd elements indices
# using filter and enumerate
res = list(filter(lambda x: test_list[x[0]] % 2 != 0, enumerate(test_list)))
           
# printing result
print("Indices list Odd elements is : " , [i[0] for i in res])
#This code is contributed by Edula Vinay Kumar Reddy


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))
#This code is contributed by Jyothi pinjala.


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.

  1. Import the numpy module using the import statement.
  2. Initialize the list test_list with some values.
  3. Print the original list using the print() statement.
  4. Create a numpy array from the list using the np.array() function.
  5. 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.
  6. 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.
  7. Store the array of indices in the variable res.
  8. Print the result using the print() statement.

Python3




# Python3 code to demonstrate working of
# Odd elements indices
# using numpy module
 
import numpy as np
 
# initialize list
test_list = [5, 6, 10, 4, 7, 1, 19]
 
# printing original list
print("The original list is : " + str(test_list))
 
# creating numpy array from list
arr = np.array(test_list)
 
# getting boolean array for odd elements
bool_arr = arr % 2 != 0
 
# getting indices of True elements using numpy's where function
res = np.where(bool_arr)[0]
 
# printing result
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
 
# initialize list
test_list = [5, 6, 10, 4, 7, 1, 19]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Odd elements indices using itertools
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]
 
# printing result
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:

  1. Initialize a list “test_list” with some elements.
  2. Print the original list “test_list”.
  3. Use list comprehension to create a new list “res” which contains the indices of all odd elements in “test_list”.
  4. Print the list “res”.

Python3




import heapq
 
# initialize list
test_list = [5, 6, 10, 4, 7, 1, 19]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using heapq to find indices of odd elements
res = [i for i, elem in enumerate(test_list) if elem % 2 != 0]
 
# printing result
print("Indices list of odd elements is : ", res)
#This code is contributed by Rayudu.


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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments