Python – Test if elements of list are in Min/Max range from other list


When it is required to test if the elements are in the min/max range, the list elements are iterated over, and are checked to see if it is equal to ‘max’ value.

Example

Below is a demonstration of the same

my_list = [5, 6, 4, 7, 8, 13, 15]
print("The list is : ")
print(my_list)
range_list = [4, 7, 10, 6]

my_result = True
for elem in range_list:

   if elem!= max(my_list):
      my_result = False
      break

if(elem == True):
   print("All the elements are in the min/max range")
else:
   print("All the elements are not in the min/max range")

Output

The list is :
[5, 6, 4, 7, 8, 13, 15]
All the elements are not in the min/max range

Explanation

  • A list is defined and is displayed on the console.

  • Another list of integers is defined.

  • A variable is assigned to ‘True’.

  • The values in the list of integers are iterated over.

  • If the maximum of the elements in the original list is not equal to any element in the list of integers, the result variable is set to ‘False’.

  • It breaks out of the loop.

  • In the end, it is checked to see if the value is ‘True’ or not.

  • Depending on this, the relevant result is displayed on the console.

Updated on: 20-Sep-2021

409 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements