Python - Find Numbers in Range and not in Set
Last Updated :
25 Apr, 2023
Given a set and range of numbers, the task is to write a Python program to extract all numbers in the range not in set.
Examples:
Input : test_set = {6, 4, 2, 7, 9}, low, high = 5, 10
Output : [5, 8]
Explanation : 6, 7 and 9 are present in set, remaining 5, 8 are in output.
Input : test_set = {6, 4, 2, 7, 9}, low, high = 5, 8
Output : [5]
Explanation : 6 and 7 are present in set, remaining 5 is in output.
Method #1: Using loop
In this, we iterate for all the elements in range and using conditional statements omit the elements from result which are not present in set.
Python3
# Python3 code to demonstrate working of
# Range Numbers not in set
# Using loop
# initializing set
test_set = {6, 4, 2, 7, 9}
# printing original set
print("The original set is : " + str(test_set))
# initializing range
low, high = 5, 10
res = []
for ele in range(low, high):
# getting elements not in set
if ele not in test_set:
res.append(ele)
# printing result
print("Elements not in set : " + str(res))
OutputThe original set is : {2, 4, 6, 7, 9}
Elements not in set : [5, 8]
Method #2: Using "-" operator
In this, we perform the task of performing getting differences from range through set elements using the "-" operator.
Python3
# Python3 code to demonstrate working of
# Range Numbers not in set
# Using "-" operator
# initializing set
test_set = {6, 4, 2, 7, 9}
# printing original set
print("The original set is : " + str(test_set))
# initializing range
low, high = 5, 10
# using "-" operator to get difference
res = list(set(range(low, high)) - test_set)
# printing result
print("Elements not in set : " + str(res))
OutputThe original set is : {2, 4, 6, 7, 9}
Elements not in set : [8, 5]
Method #3 : Using Counter() function
Python3
# Python3 code to demonstrate working of
# Range Numbers not in set
# Using loop
from collections import Counter
# initializing set
test_set = {6, 4, 2, 7, 9}
freq = Counter(test_set)
# printing original set
print("The original set is : " + str(test_set))
# initializing range
low, high = 5, 10
res = []
for ele in range(low, high):
# getting elements not in set
if ele not in freq.keys():
res.append(ele)
# printing result
print("Elements not in set : " + str(res))
OutputThe original set is : {2, 4, 6, 7, 9}
Elements not in set : [5, 8]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using list comprehension
Here, the range() function is used to generate a range of numbers from low to high (inclusive), and a list comprehension is used to iterate over the range of numbers and only include those that are not present in the given set test_set. The resulting list of missing numbers is then returned.
Python3
# Define a function that takes in a set of integers and two integers specifying a range
def find_missing_numbers(test_set, low, high):
# Use a list comprehension to generate a list of integers in the range from low to high,
# excluding any integers that are in the test_set
return [i for i in range(low, high) if i not in test_set]
# Define the range and test set of integers
low, high = 5, 10
test_set = {2, 4, 6, 7, 9}
# Call the find_missing_numbers function with the test_set and range, and print the output
print(find_missing_numbers(test_set, low, high))
Time Complexity: O(n)
Auxiliary Space: O(1)
Method #5: Using filter() and lambda function with set membership:
Algorithm:
- Initialize a set of numbers (test_set) and a range of numbers (low and high).
- Use the range() function to generate a sequence of numbers from low to high (excluding high).
- Use the filter() function to apply a lambda function to each number in the range, and return only those that are not in the test_set.
- Convert the filtered sequence to a list using the list() function.
- Print the resulting list of numbers not in the test_set.
Example:
Python3
# initializing set
test_set = {6, 4, 2, 7, 9}
# printing original set
print("The original set is : " + str(test_set))
low, high = 5, 10
res = list(filter(lambda x: x not in test_set, range(low, high)))
# printing result
print("Elements not in set : " + str(res))
OutputThe original set is : {2, 4, 6, 7, 9}
Elements not in set : [5, 8]
Time complexity: O(n), where n is the size of the range from low to high. This is because the filter() function applies the lambda function to each element in the range, so the time complexity is proportional to the size of the range.
Auxiliary Space: O(n), where n is the size of the range from low to high. This is because the resulting list of filtered numbers has to be stored in memory.
Method #6: Using Set Difference
Python3
# Python3 code to demonstrate working of
# Range Numbers not in set
# Using set difference method
# initializing set
test_set = {6, 4, 2, 7, 9}
# printing original set
print("The original set is : " + str(test_set))
# initializing range
low, high = 5, 10
# using set difference to get elements not in set
res = set(range(low, high)) - test_set
# converting set to list and sorting
res = sorted(list(res))
# printing result
print("Elements not in set : " + str(res))
OutputThe original set is : {2, 4, 6, 7, 9}
Elements not in set : [5, 8]
Time complexity O(high-low) because creating a set of the range and taking set differences takes linear time complexity in the length of the range.
Auxiliary space: O(high-low) for creating the set of the range, O(n) for the test_set, and O(high-low) for the result list.
Similar Reads
Python | Find missing numbers in a sorted list range
Given a range of sorted list of integers with some integers missing in between, write a Python program to find all the missing integers. Examples: Input : [1, 2, 4, 6, 7, 9, 10] Output : [3, 5, 8] Input : [5, 6, 10, 11, 13] Output : [7, 8, 9, 12] Method #1: List comprehension Python # Python3 progra
5 min read
Dictionary items in value range in Python
In this article, we will explore different methods to extract dictionary items within a specific value range. The simplest approach involves using a loop.Using LoopThe idea is to iterate through dictionary using loop (for loop) and check each value against the given range and storing matching items
2 min read
Print all even numbers in a range - Python
Our task is to print all even numbers within a given range. The simplest way to achieve this is by using a loop to iterate through the range and check each number for evenness. Let's explore some methods to do so.Using LoopWe can use a for loop with if conditional to check if a number is even.Python
2 min read
Create List of Numbers with Given Range - Python
The task of creating a list of numbers within a given range involves generating a sequence of integers that starts from a specified starting point and ends just before a given endpoint. For example, if the range is from 0 to 10, the resulting list would contain the numbers 0, 1, 2, 3, 4, 5, 6, 7, 8
3 min read
Print all Negative Numbers in a Range - Python
We are given a range we need to print all negative number within specific range. For example, we are given start and end point start, end = -5, 0 we need to print all numbers so that output should be -5 -4 -3 -2 -1.Using a loopWe can use a loop to iterate through a given range and check if each numb
3 min read
Print all Strong Numbers in Given List - Python
The task of printing all Strong numbers from a given list in Python involves iterating through the list and checking each number based on its digit factorial sum. A Strong number is a number whose sum of the factorials of its digits equals the number itself. For example, given a list a = [145, 375,
3 min read
Python program to find String in a List
Searching for a string in a list is a common operation in Python. Whether we're dealing with small lists or large datasets, knowing how to efficiently search for strings can save both time and effort. In this article, weâll explore several methods to find a string in a list, starting from the most e
3 min read
Python - Specific Range Addition in List
Sometimes, while working with Python, we need to perform an edition in the Python list. And sometimes we need to perform this to a specific index range in it. This kind of application can have applications in many domains. Let us discuss certain ways in which this task can be performed. Method #1:
4 min read
Python Program to Find Numbers Divisible by 7 and Multiple of 5 in a Given Range
Given a range of numbers, the task is to write a Python program to find numbers divisible by 7 and multiple of 5. Example: Input:Enter minimum 100 Enter maximum 200 Output: 105 is divisible by 7 and 5. 140 is divisible by 7 and 5. 175 is divisible by 7 and 5. Input:Input:Enter minimum 29 Enter maxim
5 min read
Python | Find groups of strictly increasing numbers in a list
Given a list of integers, write a Python program to find groups of strictly increasing numbers. Examples: Input : [1, 2, 3, 5, 6] Output : [[1, 2, 3], [5, 6]] Input : [8, 9, 10, 7, 8, 1, 2, 3] Output : [[8, 9, 10], [7, 8], [1, 2, 3]] Approach #1 : Pythonic naive This is a naive approach which uses a
5 min read