Sitemap
Vrushali Walekar

Vrushali Walekar

Built-in Set function in Python

6 min readNov 5, 2023

--

Hiii, guys I am vrushali here, in this artical we’ll learn how to use the set function in python.

Recommending readers to follow and read my article to guide on Built-in Datatype and their built-in functions in python is a helpful suggestion https://medium.com/@wvrushali27/built-in-data-types-in-python-b51b907bf536. This is especially for those who are new to python and do not know about the what are Datatypes and their built-in function in python then above link is for you guys, you just click on link and read the content in this article. If you find something usefull in this article, please like and comment.

Python has a set of built-in methods that you can use on set. Python Set Methods has multiple methods to work with Python set, Below we’ve explained all the methods you can use with python set.

let’s get start to see one by one built in function in python Set datatype.

We see all the Function in set using the dir() method .

Press enter or click to view image in full size

1.add():

Python add() method is used to add an element to set. This has no effect if the element is already present.

Syntax:

set.add (element)

  • element : Required, The element to add to the set.
Press enter or click to view image in full size

2. clear():

.The clear() method is built-in method in python set. The method clear() is used for remove all the element from set.

Syntax :

set. clear ()

Press enter or click to view image in full size

3. copy():

The copy() method returns a shallow copy of the set in python. If we use “=” to copy a set to another set, when we modify in the copied set, the changes are also reflected in the original set. So we have to create a shallow copy of the set such that when we modify something in the copied set, changes are not reflected back in the original set.

Synatx:

set.copy()

  • The copy() method for sets doesn’t take any parameters.
Press enter or click to view image in full size

4. differance():

The function difference() returns a set that is the difference between two sets. Let’s try to find out what will be the difference between two sets A and B. 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.

Synatx :

set _A. differance(set_B)

Press enter or click to view image in full size

5. differance_update():

The difference_update() method helps in an in-place way of differentiating the set. The set_differance method update the existing caller set. If A and B are two sets. The set difference() method will get the (A — B) and will return a new set. It means (A — B) is performed, then A gets modified into (A — B) means the A is changed with new set and the B remains same and if (B — A) is performed, then B gets modified into (B — A).

Syntax:

A.differance_update(B)

Press enter or click to view image in full size

6. discard():

The python discard() method is used for discard the element from the set and return a new set.

Syntax:

set.discard (element)

  • element : It is element present in given set , which we want to delete.
Press enter or click to view image in full size

In above example, the s4 is set having integer type element and i want to delete 89 from s4 set that’s why using discard method. In [39] gives a new set it’s not having 89 .

7. Intersection():

The intersection() method returns a set that contains the common element between two or more sets. The intersection of two given sets A and B is a set which consists of all the elements which are common to both A and B.

Syntax:

set1.intersection(set2, set3, set4….)
Parameters:

  • any number of sets can be passed
Press enter or click to view image in full size

8. intersection_update():

Updates the existing caller set with the intersection of sets. If set A and B are two set. The set intersection() method will get the common value and will return a new set. It means A intersection B is performed, then A gets modified into new set and the B remains same and if (B intersection A) is performed, then B gets modified and A remains same.

Syntax:

Get Vrushali Walekar’s stories in your inbox

Join Medium for free to get updates from this writer.

set1.intersection_update(set2)

Press enter or click to view image in full size

9. isdisjoint():

The python isdisjoint() method is used to return a True if two sets have a null intersection which means in both sets not have any common value otherwise it returns a false.

Syntax:

Press enter or click to view image in full size

10. issubset():

Python set issubset() method returns True if all elements of a set A are present in another set B which is passed as an argument, and returns False if all elements are not present.

Syntax:

set_obj.issubset(other_set)

Press enter or click to view image in full size

11. issuperset():

Python Set issuperset() method returns True if all elements of a set B are in set A. Then Set A is the superset of set B.

Syntax:

A.issuperset(B)

Press enter or click to view image in full size

12.pop():

Python set pop() removes any random element from the set and returns the removed element. Returns the popped element from the set and is the set is empty then raises KeyError.

Syntax:

set_obj.pop()

Press enter or click to view image in full size

13. remove():

The python remove() method is used to remove the element from the set it must be a member. If the element is not a member then raises a KeyError.

Syntax:

set.remove (set_element)

Press enter or click to view image in full size

14.symmetric_differance():

The symmetric difference of two sets set1 and set2 is the set of elements which are in either of the sets set1 or set2 but not in both.It only takes a single set as the parameter. If a list, tuple or dictionary is passed it converts it a set and performs the task.

Syntax:

set1_name.symmetric_differance(set2_name)

Press enter or click to view image in full size

15. symmetric_differance_update():

The symmetric difference between the two sets is the set of elements that are in either of the sets but not in both of them. The symmetric_difference_update() method updates the set by calling symmetric_difference_update() with the symmetric difference of sets. It means the left hand side set will get change and the right hand side set will remains same.

Syntax:

A. symmetric_difference_update (B)

Press enter or click to view image in full size

16. union():

Python set Union() Method returns a new set that contains all the items from the original set.

The union of two given sets is the set that contains all the elements of both sets. The union of two given sets A and B is a set that consists of all the elements of A and all the elements of B such that no element is repeated.

Syntax:

set1.union(set2, set3, set4….)

Press enter or click to view image in full size

17. update():

Python update() function in set adds elements from a set (passed as an argument) to the set. If the element is present in both of the sets then the element comes only one time in the updated set.

Syntax:

set1.update(set2)

Press enter or click to view image in full size

Update() method takes any number of argument. The arguments can be a set, list, tuples or a dictionary. It automatically converts into a set and adds to the set.

#vevcodelab

http://vevcodelab.com/

--

--