Friday, October 24, 2025
HomeLanguagesPython | K Divided Indices List

Python | K Divided Indices List

Sometimes, while working with Python lists, we can have a problem in which we wish to find Modulo K 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 modulo K elements. 
Method #1 : Using loop This is brute force method in which this task can be performed. In this, we check for % K element in list and append its index accordingly. 

Python3




# Python3 code to demonstrate working of
# K Dividend Indices List
# using loop
 
# initialize list
test_list = [5, 6, 10, 4, 7, 1, 19]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing K
K = 5
 
# K Dividend Indices List
# using loop
res = []
for idx, ele in enumerate(test_list):
    if ele % K == 0:
        res.append(idx)
         
# printing result
print("Indices list modulo K elements is : " + str(res))


Output

The original list is : [5, 6, 10, 4, 7, 1, 19]
Indices list modulo K elements is : [0, 2]

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 list “test_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
# K Dividend Indices List
# 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))
 
# initializing K
K = 5
 
# K Dividend Indices List
# using list comprehension
res = [idx for idx, ele in enumerate(test_list) if ele % K == 0]
         
# printing result
print("Indices list modulo K elements is : " + str(res))


Output

The original list is : [5, 6, 10, 4, 7, 1, 19]
Indices list modulo K elements is : [0, 2]

Time Complexity: O(n) where n is the number of elements in the test_list. The list comprehension is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the test list.
 

 Method #3 : Using Filter and Lambda function
 

Python3




#Python3 code to demonstrate working of
#K Dividend Indices List
#using filter and lambda function
#initialize list
test_list = [5, 6, 10, 4, 7, 1, 19]
 
#printing original list
print("The original list is : " + str(test_list))
 
#initializing K
K = 5
 
#K Dividend Indices List
#using filter and lambda function
res = list(filter(lambda x : test_list[x[0]] % K == 0, enumerate(test_list)))
 
#extracting indices from resultant tuples
res = [ele[0] for ele in res]
 
#printing result
print("Indices list modulo K elements is : " + str(res))


Output

The original list is : [5, 6, 10, 4, 7, 1, 19]
Indices list modulo K elements is : [0, 2]

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

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

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS