When it is required to customize the lower bound on a list, a list comprehension can be used and a specific condition can be placed in it.
Below is a demonstration of the same −
Example
my_list = [51, 71, 86, 21, 11, 35, 67] print("The list is :") print(my_list) K = 50 print("The value of K is ") print(K) my_result = [element if element >= K else K for element in my_list] print("The result is :") print(my_result)
Output
The list is : [51, 71, 86, 21, 11, 35, 67] The value of K is 50 The result is : [51, 71, 86, 50, 50, 50, 67]
Explanation
A list of integers is defined and is displayed on the console.
A value for K is defined and is displayed on the console.
A list comprehension is used to iterate over the elements and check if an element in the list is greater than or equal to K.
If yes, the element is stored in a list, otherwise the value of K is stored in the list.
This is assigned to a variable.
This is displayed as output on the console.