Python Indices of numbers greater than K


In this tutorial, we are going to find the indices of the numbers that are greater than the given number K. Let's see the different ways to find them.

A most common way to solve the problem is using the loops. Let's see the steps to solve the problem.

  • Initialize the list and K.
  • Iterate over the list using its length.
  • If you find any number greater than K, then print the current index.

Example

 Live Demo

# initializing the list and K
numbers = [3, 4, 5, 23, 12, 10, 16]
K = 10
# iterating over thAe list
for i in range(len(numbers)):
   # checking the number greater than K
   if numbers[i] > K:
      # printing the number index
      print(i, end=' ')

Output

If you run the above code, then you will get the following result.

3 4 6

Let's solve the problem using enumerate function. It gives you a tuple for each iteration that includes the element's index and element.

Example

 Live Demo

# initializing the list and K
numbers = [3, 4, 5, 23, 12, 10, 16]
K = 10
# finding indexes of the numbers greater than K
result = [index for (index, number) in enumerate(numbers) if number > K]
# printing the indices
print(*result)

Output

If you run the above code, then you will get the following result.

3 4 6

Conclusion

If you have any doubts in the tutorial, mention them in the comment section.

Updated on: 07-Jul-2020

429 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements