w3resource

Python: Find the indexes of numbers, below a given threshold

Python Programming Puzzles: Exercise-11 with Solution

Indices Below Threshold

Write a Python program to find the indexes of numbers in a given list below a given threshold.

Input:
[(100,(0, 12, 45, 3, 4923, 322, 105, 29, 15, 39, 55))]
Output:
[0, 1, 2, 3, 7, 8, 9, 10]

Input:
[(10,(0, 12, 4, 3, 49, 9, 1, 5, 3))]
Output:
[0, 2, 3, 5, 6, 7, 8]

Visual Presentation:

Python: Find the indexes of numbers, below a given threshold.

Sample Solution:

Python Code:

# License: https://bit.ly/3oLErEI

# Define a function named 'test' that takes a list of numbers 'nums' and a threshold 'thresh' as input
def test(nums, thresh):
    # Use a list comprehension to find the indexes (i) of numbers in 'nums' that are below the given threshold 'thresh'
    return [i for i, n in enumerate(nums) if n < thresh]

# Create a list of numbers 'nums' with specific elements
nums = [0, 12, 45, 3, 4923, 322, 105, 29, 15, 39, 55]

# Assign a threshold value 'thresh' to the variable
thresh = 100

# Print the original list of numbers
print("Original list:")
print(nums)

# Print the threshold value
print("Threshold: ", thresh)

# Print a message indicating the operation to be performed on the list
print("Check the indexes of numbers of the said list below the given threshold:")

# Print the result of the test function applied to the 'nums' list with the given threshold value
print(test(nums, thresh))

# Create a different list of numbers 'nums' with specific elements
nums = [0, 12, 4, 3, 49, 9, 1, 5, 3]

# Assign a different threshold value 'thresh' to the variable
thresh = 10

# Print the original list of numbers
print("\nOriginal list:")
print(nums)

# Print the updated threshold value
print("Threshold: ", thresh)

# Print a message indicating the operation to be performed on the list
print("Check the indexes of numbers of the said list below the given threshold:")

# Print the result of the test function applied to the modified 'nums' list with the updated threshold value
print(test(nums, thresh))

Sample Output:

Original list:
[0, 12, 45, 3, 4923, 322, 105, 29, 15, 39, 55]
Threshold:  100
Check the indexes of numbers of the said list below the given threshold:
[0, 1, 2, 3, 7, 8, 9, 10]

Original list:
[0, 12, 4, 3, 49, 9, 1, 5, 3]
Threshold:  10
Check the indexes of numbers of the said list below the given threshold:
[0, 2, 3, 5, 6, 7, 8]

Flowchart:

Flowchart: Python - Find the indexes of numbers, below a given threshold.

Python Code Editor :

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Separate Parentheses Groups Perfectly.
Next: Test whether the given strings are palindromes.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/puzzles/python-programming-puzzles-11.php