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.

Updated on: 29-Aug-2023

39 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements