Python – Custom Lower bound a List

When working with numerical data, you may need to set a custom lower bound for a list. This means replacing any values below a threshold with the threshold value itself. Python's list comprehension provides an elegant solution for this task.

Syntax

result = [element if element >= threshold else threshold for element in original_list]

Example

Let's apply a lower bound of 50 to a list of integers ?

numbers = [51, 71, 86, 21, 11, 35, 67]

print("Original list:")
print(numbers)

threshold = 50
print(f"Lower bound threshold: {threshold}")

result = [element if element >= threshold else threshold for element in numbers]

print("List with custom lower bound:")
print(result)
Original list:
[51, 71, 86, 21, 11, 35, 67]
Lower bound threshold: 50
List with custom lower bound:
[51, 71, 86, 50, 50, 50, 67]

How It Works

The list comprehension uses a conditional expression to check each element:

  • If element >= threshold, keep the original element

  • Otherwise, replace it with the threshold value

  • This ensures no value in the result is below the specified lower bound

Alternative Using max() Function

You can achieve the same result using the max() function ?

numbers = [51, 71, 86, 21, 11, 35, 67]
threshold = 50

result = [max(element, threshold) for element in numbers]

print("Using max() function:")
print(result)
Using max() function:
[51, 71, 86, 50, 50, 50, 67]

Conclusion

Use list comprehension with conditional expressions or the max() function to apply custom lower bounds. Both methods efficiently ensure all values meet your minimum threshold requirement.

Updated on: 2026-03-26T01:00:56+05:30

539 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements