In this tutorial, we will discuss the concept of Set datatype in Python programming language.
Set is a data type in Python which is an unordered collection of unique elements. Set has no duplicates and we can add or remove from the set.
How to make a set in python?
Set is a Datatype in Python. When creating a set, we can place element inside the curly braces {}, and elements separated by a comma, or we can use the pre-defined function set(). Set can have different data types (float, integer, string, tuple) and any number of items.
Example of set
when the above code is executed, it produces the following results
set([24, 36, 12, 48])
Set can not have duplicate elements
When the above code is executed, it produces the following results
set([32, 1, 43, 21, 54, 56])
Only display single elements
How to add Elements to set
We can update a single element using add() method
When the above code is executed, it produces the following results
set([8, 2, 4, 6])
set([8, 1, 2, 4, 6])
How to updating set elements
We can update multiple-element using update() method, We can get tuples, list, strings or other sets as its argument.
When the above code is executed, it produces the following results
set([8, 2, 4, 6])
set([1, 2, 3, 4, 5, 6, 7, 8])
How to remove or discard element from set use remove() function A particular item can be removed from the set using remove()method
When the above code is executed, it produces the following results
set([15, 41, 11, 21, 13])
set([15, 11, 21, 13])
use discard() function
A particular item can be removed from the set using discard() method
When the above code is executed, it produces the following results set([15, 41, 11, 21, 13])
set([15, 41, 11, 21]) How to clear the element from the set All item can be cleared from the set using clear() method
when the above code is executed, it produces the following results
How to pop element from the set We can remove an item using the pop() method
When the above code is executed, it produces the following results set([‘e’, ‘d’, ‘H’, ‘l’, ‘o’, ‘r’, ‘w’]) e
d
Set Operations
set union
Set union of X and Y is a set of all elements from both sets.
The union is performed using | operator and same can be fulfilled by the union() pre-defined method in python.
Example 1
when the above code is executed, it produces the following results
(‘union of x and y is’, set([1, 2, 3, 4, 5, 6, 7, 8]))
Find union using union() function
when the above code is executed, it produces the following results
set([1, 2, 3, 4, 5, 6, 7, 8])
set([1, 2, 3, 4, 5, 6, 7, 8])
Set Intersection
Set intersection is performed using & operator and same can be fulfilled by the intersection() pre-defined method in python.
The set intersection of X and Y is a set of the element that is common to both.
Program 1
When the above code is executed, it produces the following results.
set([4, 7])
set([4, 7])
program 2
Find the intersection using intersection() build-in function
When the above code is executed, it produces the following results
set([4, 7])
set([4, 7])
Set Difference
The difference is performed using – operator and same can be fulfilled by difference() in pre-defined method in python.
The difference between X and Y (X-Y) is a set of the element in X but without in Y
Program 1
When the above code is executed, it produces the following results
set([8, 2, 6])
set([1, 3, 5])
Find the difference using difference() build-in function
Program 2
When the above code is executed, it produces the following results
set([8, 2, 6])
set([1, 3, 5])
Set Symmetric Difference
Symantec difference is performed using ^ operator and same can be fulfilled by Symentic_difference() in pre-defined method in python.
Symantec difference of X and Y is a set of the element in both X and Y without those that are common to both.
Program 1
When the above code is executed, it produces the following results.
set([1, 2, 3, 5, 6, 8])
Program 2
Find the symmetric_difference use symmetric_difference() build in function
When the above code is executed, it produces the following results.