- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python β Check Element for Range Occurrences
The range is something that has a limited number of elements and some may be mentioned along with the starting and ending elements The methods to check the range occurrences can be varying and some may be a simple way on the other side it can be determined using a library. In Python programming, there are different ways to find whether an element is present inside the given range or not and it can be done using various methods.
Check Element for Range Occurrences
Some popular methods to check for the occurrences are discussed and there are also ways to check elements for range occurrences using the filter function which is used to check for the matches in the dictionary and the βinβ keyword is also used.
Approach
Approach 1 β Using the methods of flags
Approach 2 β Using enumerate() method
Approach 3 β Using binary search
Approach 1: Python program to check for range occurrences using iteration method of flags
The Dictionary data structure is used to define the inputs along with the key value and then the Boolean variable is initialized with a value. The addon_list is the value to match the elements to the list and the variable is set to flag of True value. To print the statements, an if else loop is used.
Algorithm
Step 1 β The dictionary is initialized with a string value along with the key of integers.
Step 2 β The element that needs to be checked whether it is present inside the dictionary is initialized as addon_list.
Step 3 β The flag variable is initialized as false.
Step 4 β To iterate through for loop to check each element of the list.
Step 5 β The printing statement is used to print according to the occurrences.
Example
#Dictionary is initialized with variables item_list = {'pencil':10,'pen':20,'book':30} #element that is checked inside the main_list addon_list =78 flag=False #iteration using for loop through each element of the list for items,item_val in item_list.items(): if item_val==addon_list: flag=True break; #if else statement is used to find the matches found or not if flag ==True: print("Element Match is Found!") else : print("Element Match is not Found!!")
Output
Element Match is not Found!!
Approach 2: Python program to check for range occurrences using the enumerate() method
The elements within the quotes are the items and the integer indicates the number of items. The enumerate() method is used to check for the matches in the dictionary. To print the statements, if else loop is used.
Algorithm
Step 1 β The input is initialized within dictionary values to hold both integers and strings.
Step 2 β The integer input is stored in a variable to check whether it is available inside the dictionary data structure.
Step 3 β To iterate through for loop to check each element of the list using enumerate() method.
Step 4 β Later, the print statement returns the result.
Example
#dictionary is initialized with strings and key-value item_list = {'pencil':10,'pen':20,'book':30} #element that is checked inside the item_list addon_list = 78 # for loop is used to find the matches found or not using enumerate() method for index, item in enumerate(item_list.values()): #if else statement prints the statement if item == addon_list: print("Element Match is Found!") break else: print("Element Match is not Found!!")
Output
Element Match is not Found!
Approach 3: Python program to check for range occurrences using binary search
The dictionary data structure is used to define the inputs along with the key value. The binary search function is defined as taking an array with two integers as maximum, minimum, and βaβ value. When this value is present it returns -1 and again iterates through the values.
Algorithm
Step 1 β The dictionary data structure is initialized with a range of values.
Step 2 β The other variable that needs to be searched is declared inside the addon_list.
Step 3 β The function has four arguments defined to look at each element in the dictionary with the help of the binary search method.
Example
#input is initialized dict1 = {'pencil':10,'pen':20,'book':30} #element that is checked inside the input addon_list = 30 # binary search method is exclusively used to check def bin_method(val, minimum, maximum, a): if maximum >= minimum: cen = (maximum + minimum) // 2 if val[cen] == a: return cen elif val[cen] > a: return bin_method(val, minimum, maximum - 1, a) else: return bin_method(val, cen + 1, maximum, a) else: return -1 #if loop is defined to find the matches found or not using the binary search method if bin_method(list(dict1.values()), 0, len(dict1) - 1, addon_list) != -1: print("Element Match is Found!") else: print("Element Match is not Found!!")
Output
Element Match is Found!
Conclusion
The Dictionary data structure is used to define the inputs along with the key value. The elements within the quotes are the items and the integer indicates the number of items. The various techniques to search for the given elements are done using the binary search, iteration method, and the help of enumerate method.