
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Numbers in Range and Not in Set using Python
In python we can use not operator or subtraction operation as well as counter function to the number that are in range but not in set.
A Python set is a grouping of unsorted elements. The set's elements must all be distinct, and unchangeable, and the sets must eliminate any duplicates. Sets can be changed after they are created because they are mutable.
Example
Assume we have taken an input set and range
Input
inputSet = {3, 10, 2, 11, 15, 4, 1} lowIndex, highIndex = 0, 8
Output
[0, 5, 6, 7]
Here 0,5,6,7 are the elements that exist in the given range and don't exists in the set.
Using the for loop and not operator
The range() function returns a sequence of numbers that starts at 0 and increments by 1 (default) and stops before a given number.
not operator(a logical operator that returns True if the statement/statements are not True else returns False)
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
Create a variable to store the input set.
Print the input set.
Create two separate variables for storing the input low and high indexes.
Create an empty list for storing resultant numbers that don't exist in input set.
Use the for loop to traverse in a range from the input low and high index using range() function.
Use the if conditional statement to check whether the current index is not in the input set using the not operator.
Use the append() function(adds the element to the list at the end) to append the current element(index) to the resultant list.
Print the resultant list of elements not in a set within the given input range.
Example
The following program returns a list of numbers in a given input range not in an input set using the for loop and not operator -
# input set inputSet = {3, 10, 2, 11, 15, 4, 1} # printing input set print("Input set:", inputSet) # input low and high indexes lowIndex, highIndex = 0, 8 # resultant list for storing numbers, not in the set resultantList = [] # travsersing in a range from input low and high index for i in range(lowIndex, highIndex): # checking whether the current index does not exist in the input set if i not in inputSet: # appending the current element to the resultant list resultantList.append(i) # printing the resultant list of elements not in a set # within the specified range print("Resultant list of elements not in a set within the specified range:\n", resultantList)
Output
On executing, the above program will generate the following output -
Input set: {1, 2, 3, 4, 10, 11, 15} Resultant list of elements not in a set within the specified range: [0, 5, 6, 7]
Using the "-" operator
The following program returns a list of numbers in a given input range not in an input set using the "-" operator -
Example
# input set inputSet = {3, 10, 2, 11, 15, 4, 1} # printing input set print("Input set:", inputSet) # input low and high indexes lowIndex, highIndex = 0, 8 # Converting the numbers in the range to set # Subtracting this set from the given input set resultantList = list(set(range(lowIndex, highIndex)) - inputSet) # printing the resultant list of elements not in a set print("Resultant list of elements not in a set within the specified range:\n", resultantList)
Output
On executing, the above program will generate the following output -
Input set: {1, 2, 3, 4, 10, 11, 15} Resultant list of elements not in a set within the specified range: [0, 5, 6, 7]
Using the Counter() function
Counter() function ? a sub-class that counts the hashable objects. It implicitly creates a hash table of an iterable when called/invoked.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
Use the import keyword to import the Counter function from collections module.
Use the Counter() function to get the frequency of elements of the input set as a key-value pair.
Create two separate variables for storing the input low and high indexes.
Create an empty list for storing resultant numbers, not in the input set.
Use the for loop to traverse in a range from input low and high index.
Use the if conditional statement to check whether the current element is not in keys of the set frequency with the keys() function.
Use the append() function to append that current element to the resultant list.
Print the resultant list of elements not in a set within the given input range.
Example
The following program returns a list of numbers in a given input range not in an input set using the Counter() function -
# importing Counter from collections from collections import Counter # input set inputSet = {3, 10, 2, 11, 15, 4, 1} # printing input set print("Input set:", inputSet) # getting the frequency of elements of the input set setFrequency = Counter(inputSet) # input low and high indexes lowIndex, highIndex = 0, 8 # resultant list for storing numbers, not in the set resultantList = [] # travsersing in a range from input low and high index for i in range(lowIndex, highIndex): # checking whether the current element is not in keys of the set frequency if i not in setFrequency.keys(): # appending current item to the resultant list resultantList.append(i) print("Resultant list of elements not in a set within the specified range:\n", resultantList)
Output
On executing, the above program will generate the following output -
Input set: {1, 2, 3, 4, 10, 11, 15} Resultant list of elements not in a set within the specified range: [0, 5, 6, 7]
Conclusion
In this article, we have learned how to Find Numbers in Range and not in Set using 3 different approaches. Additionally, we learned how to use the range() function to obtain the range of numbers between the specified indices. Finally, we learned how to utilize the Counter() function to quickly and efficiently(O(1) Time Complexity) determine whether an element is present in the given set.