Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to Find Numbers in Range and not in Set
In Python, we can find numbers that exist within a specific range but are not present in a given set using several approaches: the not operator, set subtraction, or the Counter function.
A Python set is a collection of unordered, unique elements. Sets are mutable, meaning you can add or remove elements after creation, but the elements themselves must be immutable and distinct.
Problem Example
Given an input set and a range, find all numbers in the range that don't exist in the set ?
Input
input_set = {3, 10, 2, 11, 15, 4, 1}
low_index, high_index = 0, 8
Expected Output
[0, 5, 6, 7]
Here 0, 5, 6, 7 are the numbers that exist in the range [0, 8) but don't exist in the set.
Using For Loop and Not Operator
The range() function generates a sequence of numbers, and the not operator checks if an element is absent from the set ?
# Input set
input_set = {3, 10, 2, 11, 15, 4, 1}
print("Input set:", input_set)
# Define range boundaries
low_index, high_index = 0, 8
# List to store numbers not in set
result_list = []
# Check each number in range
for i in range(low_index, high_index):
if i not in input_set:
result_list.append(i)
print("Numbers in range but not in set:", result_list)
Input set: {1, 2, 3, 4, 10, 11, 15}
Numbers in range but not in set: [0, 5, 6, 7]
Using Set Subtraction
Convert the range to a set and subtract the input set to find missing numbers ?
# Input set
input_set = {3, 10, 2, 11, 15, 4, 1}
print("Input set:", input_set)
# Define range boundaries
low_index, high_index = 0, 8
# Convert range to set and subtract input set
range_set = set(range(low_index, high_index))
result_list = list(range_set - input_set)
print("Numbers in range but not in set:", result_list)
Input set: {1, 2, 3, 4, 10, 11, 15}
Numbers in range but not in set: [0, 5, 6, 7]
Using Counter Function
The Counter function creates a frequency dictionary, allowing efficient membership testing ?
from collections import Counter
# Input set
input_set = {3, 10, 2, 11, 15, 4, 1}
print("Input set:", input_set)
# Create frequency counter
set_frequency = Counter(input_set)
# Define range boundaries
low_index, high_index = 0, 8
# Find numbers not in counter keys
result_list = []
for i in range(low_index, high_index):
if i not in set_frequency.keys():
result_list.append(i)
print("Numbers in range but not in set:", result_list)
Input set: {1, 2, 3, 4, 10, 11, 15}
Numbers in range but not in set: [0, 5, 6, 7]
Comparison of Methods
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| For Loop + Not Operator | O(n×m) | O(1) | Simple logic, readable code |
| Set Subtraction | O(n+m) | O(n) | Concise, efficient for large ranges |
| Counter Function | O(n+m) | O(m) | When frequency data is needed |
Where n is the range size and m is the set size.
Conclusion
Set subtraction is the most efficient approach for finding numbers in range but not in set. Use the for loop method for simple cases or when you need to understand each step clearly.
