I have a range of numbers such as 1-100. And I have a set that holds all, or a random subset of numbers in that range such as:
s = set([1,2,3,35,67,87,95])
What is a good way to get all of the numbers in the range 1-100 that are not in that set?
In & Not in operators“not in” operator − This operator is used to check whether an element is not present in the passed list or not. Returns true if the element is not present in the list otherwise returns false.
a_list = [1, 2, 3, 4, 5] # Checks if the variable "a_list" is a list if type(a_list) == list: print("Variable is a list.") else: print("Variable is not a list.") This results in: "Variable is a list."
Python Set | difference() Then (set A – set B) will be the elements present in set A but not in B and (set B – set A) will be the elements present in set B but not in set A. Let's look at the Venn diagram of the following difference set function. We can also use – operator to find the difference between two sets.
Use set difference operation
set(range(1, 101)) - s
Set difference
set(range(1, 101)) - s
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With