Python - Indices of Atmost K Elements in List


The atmost K element is set to a specific value in the given list to filter those indices whose elements are greater than the K value. In Python, we have some built-in functions such as enumerate(), range(), len(), where(), map(), range(), and, filter() that will be used to solve the Indices of atmost K elements in list.

Let’s take an example of this.

The given list, [10, 8, 13, 29, 7, 40, 91] and the K value set to 13.

Now, it will check how many elements are less than or equal to 12 and filter those indices which are greater than of it.

Then the final result becomes [10, 8, 13, 7]

Syntax

The following syntax is used in the examples-

enumerate()

The enumerate() is an in-built function in Python that keep track of particular iteration of each given index element.

range()

The range() is an in-built function in Python that returns the sequence of numbers according to a given range.

len()

The len() is an in-built function in Python that returns the length of the result.

where()

The where() is an in-built function in Python that returns the indexes of input array element where the specific condition satisfied.

map()

The built-in function map() allows for the iteration of input list by using some specific built-in functions.

filter()

The filter() method is applied when we need to filter the items based on specific conditions. In simple terms, it allows to iterate those elements that are extracted to satisfy the condition.

lambda()

This lambda function in Python is known as an anonymous function. It can be used when function objects are required.

Using list Comprehension

In the following example, we will start the program by defining the K value that represents the atmost elements. Then create the list that will be used in the list comprehension where built-in function enumerate() keep track of the particular iteration of each index element. Using <= operator it finds some of the atmost elements according to K value and returns the result in the form of a list.

Example

k = 10
list1 = [10, 4, 11, 12, 14]
idx = [i for i, num in enumerate(list1) if num <= k]
print(idx)

Output

 [0,1]

Using a loop and Appending Indices to a New List

In the following example, the program uses for loop where variable i iterate through the input list using built-in function len() and range(). Next, use the if statement to set the condition based on atmost K element using < operator. Then use the append() method that inserts the element through variable i and prints the result.

Example

k = 40
lst = [10, 20, 50, 60, 30, 80, 90]
idx = []
for i in range(len(lst)):
    if lst[i] < k:
        idx.append(i)
print(idx)

Output

 [0, 1, 4]

Using Numpy and Boolean index

In the following example, the program uses numpy library, k value, and, input list(lst) to calculate the indices of atmost K element. The built-in array() converts the list into an array element and store it in the variable arr. Next, the built-in function where() set the condition between arr and K using < operator which returns those elements which is lesser than the value of K and store it in the variable idx. Finally, we are printing the result with the variable idx.

Example

import numpy as np
k = 12
lst = [10, 4, 11, 12, 14]
arr = np.array(lst)
idx = np.where(arr < k)[0]
print(idx)

Output

 [0 1 2]

Using enumerate() and filter()

In the following example, the program uses some built-in functions such as list(), map(), lambda(), and, enumerate() to find the indices of atmost K elements in the list.

Example

k = 8
lst = [10, 4, 11, 12, 14, 1, 2, 89, 0]
idx = list(map(lambda x: x[0], filter(lambda x: x[1] < k, enumerate(lst))))
print(idx)

Output

 [1, 5, 6, 8]

Conclusion

The atmost K elements in the list are those elements that set the specific value of K and return those elements which are less than or equal to K. There are various built-in functions used such as filter(), lambda, append(), etc. are used to solve this problem statement. This type of program is commonly used in Algorithmic-problem solving.

Updated on: 16-Aug-2023

47 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements