10.Sets In Python
What are sets?
A Python set is the collection of the unordered items. Each element in the set must be unique, immutable, and the sets remove the duplicate elements. Sets are mutable which means we can modify it after its creation.
Properties : Unordered , Mutable\Changeable , Does not allow Duplicate Values.
Sets are unordered and its item has no index numbers. It means Index numbers do not exist in Sets.
Set items are defined in enclosed curly braces {}. For Example:
In [1]:
Properties of Sets:
1.Sets are Unordered :
In sets , items of set has no index number . So there is no order in element of sets. For Example:
In [1]:
In [4]:
So from the above codes , we can figure out that there is no order in sets and items of sets have no specific position because there is no index numbers associated with set items.
Indexing and Slicing in Sets:
Because set has no indexes , so there is no indexing or slicing in set. We cannot access any particular set item. For Example:
In [5]:
Sets are Mutable :
Sets are Changeable , we can add or remove values in a set by using set method. For Example:
In [6]:
Sets do not allow duplicate values :
Sets remove duplicate values and keeps only one value in set.For Example :
In [7]:
Though we inserted 3 'Flare' in set, set will have only one 'Flare' as a single item.
Functions used in sets:
Just like other iterables (string , list , tuple) , Functions in sets are same.
In [9]:
In [10]:
In [11]:
Type of a set:
In [1]:
Membership operator in set:
In [7]:
Set Methods:
Functions that are specific to Set are known as Set methods. We will go through each method one by one.
Adding an item or a single element in a set: add()
By now we have read other data structures like list , tuples. To add an item or a single element in list , we would use append method , but in set we use add() method. There is not much difference between both.
add() method does not return any value and adds element in set.
Note:Because sets have no index number, elements that we add in set position themselves in random place.
In [18]:
Just like append() , add() method also treats parameters as a single element. so if we try to add an iterable through add() method , it will be added in set as a single element. For Example:
In [19]:
Note:We are not allowed to add a mutable data structures in a set. hence Lists , sets , Dictionary and sets are not allowed in a set.
In [20]:
In [21]:
Though We are allowed to add immutable data types in sets like numbers , strings , tuples etc.
Adding iterables elements individually : union() and update()
union():
union() method adds elements of an iterables individually in a set. It returns a set with added elements . For Example:
In [2]:
In [3]:
Hence , You can add elements of any iterable in set.
update():
update() method adds elements of an iterables individually in a set just like union.The only difference is it does not return any value and adds element in First set . For Example:
In [4]:
If i want to change set_b , i will write above code something like this.
In [6]:
How to remove set items from set using methods : remove() , discard() , pop()
remove():
remove() method removes the element from set. It takes only one argument.
Parameter :
element : An element that we want to remove.
For Example:
In [7]:
Note:remove method will throw an error if element to be removed does not exist in set.
In [9]:
discard():
discard() method removes the element from set. It takes only one argument.
Parameter :
element : An element that we want to remove.
For Example:
In [10]:
Difference between remove() and discard():
remove() and discard() works in same way. The only difference is remove throws an error if value to be removed does not exist in set , while discard does not throw any error. For Example:
In [11]:
pop():
You can also use the pop() method to remove an item, but this method will remove a random item. Remember that sets are unordered, so you will not know what item that gets removed. It has no parameters.
The return value of the pop() method is the removed item.
In [12]:
It also returns the deleted value from set. For Example:
In [13]:
difference() and difference_update():
difference() method returns the uncommon values in the set. for example:
In [14]:
In [15]:
difference_update :
difference_update removes the items in this set that are also included in another, specified set.It does not return any value.
In [16]:
In [18]:
intersection() and intersection_update():
intersection() returns common values from two sets. For Example:
In [19]:
intersection_update():
intersection_update removes uncommon values from the set. It does not return any value . For example:
In [20]:
symmetric_difference and symmetric_difference_update:
symmetric_difference:
symmetric_difference method returns a set comprises of uncommon values from both sets. For Example:
In [21]:
symmetric_difference_update :
This method changes the value of first set. where values are uncommon values from both sets. For example:
In [22]:
issubset:
subset means if all the elements of set A is present in set B. Set A is a subset of B.
issubset() method returns true if Set A is subset of Set B.
In [23]:
issuperset:
superset means if all the elements of set A is present in set B. set B is superset of set A.
issuperset() method returns true if Set B is superset of Set A. For Example:
In [24]:
isdisjoint() :
Two sets are disjoint when there is no common value between two sets. It returns True if no values are common in sets otherwise False.
In [25]:
In [26]:
clear():
To clear all the contents of set , we use clear() method.
In [27]:
copy() :
To copy all the contents of a set in another set, we use copy method. For Example:
In [28]:
set() Constructor :
set() constructor returns an iterable into set. For Example:
In [29]:
In [30]:
How to create an Empty set:
To create an empty set , we cannot use curly braces{}. Because curly braces are used to signify empty dictionary.
To create an empty set , we will use set() constructor. For Example:
In [31]:
Last updated