Skip to content

Set


A set is a mutable collection of distinct hashable objects, same as the list and tuple. It is an unordered collection of objects, meaning it does not record element position or order of insertion and so cannot access elements using indexes. listtuple The set is a Python implementation of the set in Mathematics. A set object has suitable methods to perform mathematical set operations like union, intersection, difference, etc.

A set object contains one or more items, not necessarily of the same type, which are separated by a comma and enclosed in curly brackets . The following defines a set object with even numbers.

even_nums = {2, 4, 6, 8, 10} # set of even numbers
emp = {1, 'Steve', 10.5, True} # set of different objects

even_nums = {2, 4, 6, 8, 10} # set of even numbers emp = {1, 'Steve', 10.5, True} # set of different objectsTry it A set doesn’t store duplicate objects. Even if an object is added more than once inside the curly brackets, only one copy is held in the set object. Hence, indexing and slicing operations cannot be done on a set object.

nums = {1, 2, 2, 3, 4, 4, 5, 5}
print(nums) #output: {1, 2, 3, 4, 5}

nums = {1, 2, 2, 3, 4, 4, 5, 5} print(nums) #output: {1, 2, 3, 4, 5}Try it The order of elements in the set is not necessarily the same as the order given at the time of assignment. Python optimizes the structure of a set for performing operations over it, as defined in mathematics.

Only immutable (and hashable) objects can be a part of a set object. Numbers (integer, float, as well as complex), strings, and tuple objects are accepted, but set, list, and dictionary objects are not.

myset = {(10,10), 10, 20}
print(myset)
myset = {[10, 10], 10, 20} #TypeError can't add a list
myset = { {10, 10}, 10, 20} #TypeError can't add a set

`myset = {(10,10), 10, 20} print(myset)

myset = {[10, 10], 10, 20} #TypeError can’t add a list

myset = { {10, 10}, 10, 20} #TypeError can’t add a set[Try it](/codeeditor?cid=python-3z7umnxcc) In the above example, (10,10) is a tuple, hence it becomes part of the set. However, [10,10] is a list, hence an error message is displayed saying that the list is unhashable. (Hashing is a mechanism in computer science which enables quicker search of objects in the computer's memory.) (10,10)“[10,10]`Hashing Even though mutable objects are not stored in a set, the set itself is a mutable object.

Use the set() function to create an empty set. Empty curly braces will create an empty dictionary instead of an empty set. set() functiondictionary

emp = {} # creates an empty dictionary
print(type(emp)) #<class 'dict'>
s = set() # creates an empty set
print(type(s)) #<class 'set'>

`emp = {} # creates an empty dictionary print(type(emp)) #<class ‘dict’>

s = set() # creates an empty set print(type(s)) #<class ‘set’>`Try it The set() function also use to convert string, tuple, or dictionary object to a set object, as shown below. set() function

s = set('Hello') # converts string to set
print(s) #output: {'l', 'H', 'o', 'e'}
s = set((1,2,3,4,5)) # converts tuple to set
print(s) #output: {1, 2, 3, 4, 5}
d = {1:'One', 2: 'Two'}
s = set(d) # converts dict to set
print(s) #{1, 2}

`s = set(‘Hello’) # converts string to set print(s) #output: {‘l’, ‘H’, ‘o’, ‘e’}

s = set((1,2,3,4,5)) # converts tuple to set print(s) #output: {1, 2, 3, 4, 5}

d = {1:‘One’, 2: ‘Two’} s = set(d) # converts dict to set print(s) #{1, 2}`Try it

Use built-in set functions add(), remove() or update() methods to modify set collection. add()remove()update()

s = set() # creates an empty set
s.add(10) # add an element
s.add(20)
s.add(30)
print(s) #output: {10, 20, 30}
primeNums = {2, 3, 5, 7}
s.update(primeNums) # update set with another set
print(s) #output:{2, 3, 20, 5, 7, 10, 30}
s.remove(2) # remove an element
print(s) #output:{3, 20, 5, 7, 10, 30}

`s = set() # creates an empty set s.add(10) # add an element s.add(20) s.add(30) print(s) #output: {10, 20, 30}

primeNums = {2, 3, 5, 7} s.update(primeNums) # update set with another set print(s) #output:{2, 3, 20, 5, 7, 10, 30}

s.remove(2) # remove an element print(s) #output:{3, 20, 5, 7, 10, 30}`Try it

As mentioned earlier, the set data type in Python implements as the set defined in mathematics. Various set operations can be performed. Operators |, &, - and ^ perform union, intersection, difference, and symmetric difference operations, respectively. Each of these operators has a corresponding method associated with the built-in set class. set.union()

s1=5s2=8s1|s2 #8

Try it

s1=5s2=8s1.union(s2) #8s2.union(s1) #8

Try itset.intersection()

s1=5s2=8s1&s2 #5s2&s1 #5

Try it

s1=5s2=8s1.intersection(s2) #5s2.intersection(s1) #5

Try itset.difference()

s1=5s2=8s1-s2 #3s2-s1 #7

Try it

s1=5s2=8s1.difference(s2) #3s2.difference(s1) #7

Try itset.symmetric_difference()

s1=5s2=8s1^s2 #8s2^s1 #8

Try it

s1=5s2=8s1.symmetric_difference(s2) #8s2.symmetric_difference(s1) #8

Try it

The following table lists built-in set methods: set.add()set.clear()set.copy()set.difference()set.difference_update()set.discard()set.intersection()set.intersection_update()set.isdisjoint()set.issubset()set.pop()set.remove()set.symmetric_difference()set.symmetric_difference_update()set.union()set.update()