w3resource

Python: Find the indices of elements of a given list, greater than a specified value

Python List: Exercise - 205 with Solution

Indices Greater Than Value

Write a Python program to find the indices of elements in a given list that are greater than a specified value.

Visual Presentation:

Python List: Find the indices of elements of a given list, greater than a specified value.

Sample Solution:

Python Code:

# Define a function 'test' that finds the indices of elements in the given list that are greater than a specified value.
def test(lst, value):
    # Use a list comprehension with 'enumerate' to find the indices of elements that are greater than 'value'.
    result = [i for i, val in enumerate(lst) if val > value]
    return result

# Define a list 'nums' containing integers.
nums = [1234, 1522, 1984, 19372, 1000, 2342, 7626]

# Print a message indicating the original list.
print("\nOriginal list:")
# Print the original list 'nums'.
print(nums)

# Set a value 'val'.
val = 3000

# Print a message indicating the purpose of the following lines of code.
print("Indices of elements of the said list, greater than", val)
# Call the 'test' function to find indices of elements greater than 'val' and print the result.
print(test(nums, val))

# Define a new list 'nums' with the same integer elements.
nums = [1234, 1522, 1984, 19372, 1000, 2342, 7626]

# Print a message indicating the original list.
print("\nOriginal list:")
# Print the new list 'nums'.
print(nums)

# Set a new value 'val'.
val = 20000

# Print a message indicating the purpose of the following lines of code.
print("Indices of elements of the said list, greater than", val)
# Call the 'test' function again with the new value 'val' and print the result.
print(test(nums, val))

Sample Output:

Original list:
[1234, 1522, 1984, 19372, 1000, 2342, 7626]
Indices of elements of the said list, greater than 3000
[3, 6]

Original list:
[1234, 1522, 1984, 19372, 1000, 2342, 7626]
Indices of elements of the said list, greater than 20000
[]

Flowchart:

Flowchart: Find the indices of elements of a given list, greater than a specified value.

Python Code Editor:

Previous: Write a Python program to check if first digit/character of each element in a given list is same or not.
Next: Write a Python program to remove additional spaces in a given list.

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/list/python-data-type-list-exercise-205.php