Open In App

Python Set Methods

Last Updated : 12 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

A Set in Python is a collection of unique elements which are unordered and mutable. Python provides various functions to work with Set. In this article, we will see a list of all the functions provided by Python to deal with Sets.

Adding and Removing elements

We can add and remove elements form the set with the help of the below functions – 

  • add(): Adds a given element to a set
  • clear(): Removes all elements from the set
  • discard(): Removes the element from the set
  • pop(): Returns and removes a random element from the set
  • remove(): Removes the element from the set

Example: Adding and removing elements from the Set.

Python3




# set of letters
s = {'g', 'e', 'k', 's'}
 
# adding 's'
s.add('f')
print('Set after updating:', s)
 
# Discarding element from the set
s.discard('g')
print('\nSet after updating:', s)
 
# Removing element from the set
s.remove('e')
print('\nSet after updating:', s)
 
# Popping elements from the set
print('\nPopped element', s.pop())
print('Set after updating:', s)
 
s.clear()
print('\nSet after updating:', s)


Output

Set after updating: {'g', 'k', 's', 'e', 'f'}

Set after updating: {'k', 's', 'e', 'f'}

Set after updating: {'k', 's', 'f'}

Popped element k
Set after updating: {'s', 'f'}

Set after updating: set()

 
 

Table of Python Set Methods

Functions Name Description
add() Adds a given element to a set
clear() Removes all elements from the set
copy() Returns a shallow copy of the set
difference() Returns a set that is the difference between two sets
difference_update() Updates the existing caller set with the difference between two sets
discard() Removes the element from the set
frozenset() Return an immutable frozenset object
intersection() Returns a set that has the intersection of all sets
intersection_update() Updates the existing caller set with the intersection of sets
isdisjoint() Checks whether the sets are disjoint or not
issubset() Returns True if all elements of a set A are present in another set B
issuperset() Returns True if all elements of a set A occupies set B
pop() Returns and removes a random element from the set
remove() Removes the element from the set
symmetric_difference() Returns a set which is the symmetric difference between the two sets
symmetric_difference_update() Updates the existing caller set with the symmetric difference of sets
union() Returns a set that has the union of all sets
update() Adds elements to the set

 

Note: For more information about Python Sets, refer to Python Set Tutorial.

 



Similar Reads

Python String Methods | Set 1 (find, rfind, startwith, endwith, islower, isupper, lower, upper, swapcase & title)
Some of the string basics have been covered in the below articles Strings Part-1 Strings Part-2 The important string methods will be discussed in this article1. find("string", beg, end) :- This function is used to find the position of the substring within a string.It takes 3 arguments, substring , starting index( by default 0) and ending index( by
4 min read
Python String Methods | Set 2 (len, count, center, ljust, rjust, isalpha, isalnum, isspace & join)
Some of the string methods are covered in the set 3 below String Methods Part- 1 More methods are discussed in this article 1. len() :- This function returns the length of the string. 2. count("string", beg, end) :- This function counts the occurrence of mentioned substring in whole string. This function takes 3 arguments, substring, beginning posi
4 min read
Python String Methods | Set 3 (strip, lstrip, rstrip, min, max, maketrans, translate, replace & expandtabs())
Some of the string methods are covered in the below sets.String Methods Part- 1 String Methods Part- 2More methods are discussed in this article1. strip():- This method is used to delete all the leading and trailing characters mentioned in its argument.2. lstrip():- This method is used to delete all the leading characters mentioned in its argument.
4 min read
List Methods in Python | Set 1 (in, not in, len(), min(), max()...)
List methods are discussed in this article. 1. len() :- This function returns the length of list. List = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1] print(len(List)) Output: 10 2. min() :- This function returns the minimum element of list. List = [2.3, 4.445, 3, 5.33, 1.054, 2.5] print(min(List)) Output: 1.054 3. max() :- This function returns the maximum eleme
2 min read
List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()...)
Some of the list methods are mentioned in set 1 below List Methods in Python | Set 1 (in, not in, len(), min(), max()…) More methods are discussed in this article. 1. del[a : b] :- This method deletes all the elements in range starting from index 'a' till 'b' mentioned in arguments. 2. pop() :- This method deletes the element at the position mentio
4 min read
Accessing Attributes and Methods in Python
Attributes of a class are function objects that define corresponding methods of its instances. They are used to implement access controls of the classes. Attributes of a class can also be accessed using the following built-in methods and functions : getattr() - This function is used to access the attribute of object. hasattr() - This function is us
3 min read
Python | Float type and its methods
The float type in Python represents the floating point number. Float is used to represent real numbers and is written with a decimal point dividing the integer and fractional parts. For example, 97.98, 32.3+e18, -32.54e100 all are floating point numbers. Python float values are represented as 64-bit double-precision values. The maximum value any fl
3 min read
Python | Implementing 3D Vectors using dunder methods
Dunder methods (double underscore) in Python are methods which are commonly used for operator overloading. Some examples of dunder methods are __init__ , __repr__ , __add__ , __str__ etc. These methods are useful to modify the behavior of an object. For example, when '+' operator is used between two numbers, the result obtained is simply the additi
4 min read
Private Methods in Python
Encapsulation is one of the fundamental concepts in object-oriented programming (OOP) in Python. It describes the idea of wrapping data and the methods that work on data within one unit. This puts restrictions on accessing variables and methods directly and can prevent the accidental modification of data. A class is an example of encapsulation as i
3 min read
Bound methods python
A bound method is the one which is dependent on the instance of the class as the first argument. It passes the instance as the first argument which is used to access the variables and functions. In Python 3 and newer versions of python, all functions in the class are by default bound methods. Let's understand this concept with an example: # Python
2 min read
Practice Tags :
three90RightbarBannerImg