Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python find numbers not in set

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?

like image 441
Chris Dutrow Avatar asked Apr 28 '11 19:04

Chris Dutrow


People also ask

How do you check if an element is not set in Python?

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.

How do you check if a variable is not in a list Python?

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."

How do you find elements that are only in set A or in set B but not in both in Python?

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.


2 Answers

Use set difference operation

set(range(1, 101)) - s
like image 94
Imran Avatar answered Oct 10 '22 04:10

Imran


Set difference

set(range(1, 101)) - s
like image 20
Praveen Gollakota Avatar answered Oct 10 '22 02:10

Praveen Gollakota