A lot of times, while working with data, we have a problem in which we need to make sure that a container or a list is having elements in just one range. This has application in Data Domains. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using loop
This is brute force method in which this task can be performed. In this, we just check using if condition if element falls in range, and break if we find even one occurrence out of range.
# Python3 code to demonstrate # Test if List contains elements in Range # using loop # Initializing loop test_list = [4, 5, 6, 7, 3, 9] # printing original list print("The original list is : " + str(test_list)) # Initialization of range i, j = 3, 10 # Test if List contains elements in Range # using loop res = Truefor ele in test_list: if ele < i or ele >= j : res = False break # printing result print ("Does list contain all elements in range : " + str(res)) |
The original list is : [4, 5, 6, 7, 3, 9] Does list contain all elements in range : True
Method #2 : Using all()
This is alternative and shorter way to perform this task. In this we use check operation as a parameter to all() and returns True when all elements in range.
# Python3 code to demonstrate # Test if List contains elements in Range # using all() # Initializing loop test_list = [4, 5, 6, 7, 3, 9] # printing original list print("The original list is : " + str(test_list)) # Initialization of range i, j = 3, 10 # Test if List contains elements in Range # using all() res = all(ele >= i and ele < j for ele in test_list) # printing result print ("Does list contain all elements in range : " + str(res)) |
The original list is : [4, 5, 6, 7, 3, 9] Does list contain all elements in range : True
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Test if string contains element from list
- Python - Test for all Even elements in the List for the given Range
- Python | Check if list contains all unique elements
- Python | Test if dictionary contains unique keys and values
- Python - Test if Tuple contains K
- Python | Test if String contains Alphabets and Spaces
- Python - Test if String contains any Uppercase character
- Python program to check if the list contains three consecutive common numbers in Python
- Python | Check if list contains consecutive numbers
- Python | Test if all elements are present in list
- Python - Test rear digit match in all list elements
- Python - Test if all elements in list are of same type
- Python - Check if Tuple contains only K elements
- Python | Print list elements in circular range
- Python | Assign range of elements to List
- Python - Extract range of Consecutive Similar elements ranges from string list
- Python - Test K in Range
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of String List to String List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

