PDA Assignments
  • Python For Data Analytics
    • 1.Python
      • 1.Python Documents
        • 1.Data Types
        • 2.Variables In Python
        • 3.Operators In Python
        • 4.User Input In Python
        • 5.TypeCasting In Python
        • 6.Strings In Python
        • 7.Conditional Statements In Python
        • 8.Branching using Conditional Statements and Loops in Python
        • 9.Lists In Python
        • 10.Sets In Python
        • 11.Tuples In Python
        • 12.Dictionary In Python
        • 13.Functions In Python
        • 14.File Handling In Python
        • 15.Numerical Computing with Python and Numpy
      • 2.Python Assignments
        • Data Type & Variables
        • Operators Assignment
        • User Input & Type Casting
        • Functions- Basic Assignments
        • String Assignments
          • String CheatSheet
        • Conditional Statements Assignments
        • Loops Assignments
        • List Assignments
          • List Cheatsheet
        • Set Assignments
          • Sets Cheatsheet
        • Dictionary Assignments
          • Dictionary Cheatsheet
        • Function Assignments
        • Functions used in Python
      • 3.Python Projects
        • Employee Management System
        • Hamming distance
        • Webscraping With Python
          • Introduction To Web Scraping
          • Importing Necessary Libraries
          • Basic Introduction To HTML
          • Introduction To BeautifulSoup
          • Flipkart Web Scraping
            • Scraping Step By Step
        • Retail Sales Analysis
        • Guess the Word Game
        • Data Collection Through APIs
        • To-Do List Manager
        • Atm-functionalities(nested if)
        • Distribution of Cards(List & Nested for)
        • Guess the Number Game
      • 4.Python + SQL Projects
        • Bookstore Management System
    • 2.Data Analytics
      • 1.Pandas
        • 1.Pandas Documents
          • 1.Introduction To Pandas
          • Reading and Loading Different Data
          • 2.Indexing and Slicing In Pandas
          • 3.Joining In Pandas
          • 4.Missing Values In Pandas
          • 5.Outliers In Pandas
          • 6.Aggregating Data
          • 7.DateTime In Pandas
          • 8.Validation In Pandas
          • 9.Fetching Data From SQL
          • 10. Automation In Pandas
          • 11.Matplotlib - Data Visualization
          • 12. Seaborn - Data Visualization
          • 13. Required Files
        • 3.Pandas Projects
          • Retail Sales Analysis
            • Retail Sales Step By Step
          • IMDB - Dataset Analysis - Basic
        • 2. Pandas Assignments
          • 1. Reading and Loading the Data
          • 2. Data frame Functions and Properties
          • 3. Series - Basic Operations
          • 4. Filtering in Pandas
          • 5. Advance Filtering
          • 6. Aggregate Functions & Groupby
          • 7. Pivot Tables
          • 8. Datetime
          • 9. String Functions
Powered by GitBook
On this page
  • What are sets?
  • Properties of Sets:
  • 1.Sets are Unordered :
  • Indexing and Slicing in Sets:
  • Sets are Mutable :
  • Sets do not allow duplicate values :
  • Functions used in sets:
  • Type of a set:
  • Set Methods:
  • Adding an item or a single element in a set: add()
  • Adding iterables elements individually : union() and update()
  • union():
  • update():
  • How to remove set items from set using methods : remove() , discard() , pop()
  • remove():
  • discard():
  • Difference between remove() and discard():
  • pop():
  • difference() and difference_update():
  • difference_update :
  • intersection() and intersection_update():
  • intersection_update():
  • symmetric_difference and symmetric_difference_update:
  • symmetric_difference:
  • symmetric_difference_update :
  • issubset:
  • issuperset:
  • isdisjoint() :
  • clear():
  • copy() :
  • set() Constructor :
  • How to create an Empty set:
  1. Python For Data Analytics
  2. 1.Python
  3. 1.Python Documents

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]:

varset = {"Console","Python","data","Flare"}
print(varset)
{'data', 'Python', 'Console', 'Flare'}

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]:

varset = {"Console","Python","data","Flare"}
print(varset)
{'Python', 'data', 'Flare', 'Console'}

In [4]:

varset = {"Console","Python","data","Flare"}
print(varset) 
{'Python', 'Flare', 'data', 'Console'}

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]:

varset = {"Console","Python","data","Flare"}
varset[1]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-a3fa81f31623> in <module>
      1 varset = {"Console","Python","data","Flare"}
----> 2 varset[1]

TypeError: 'set' object is not subscriptable

Sets are Mutable :

Sets are Changeable , we can add or remove values in a set by using set method. For Example:

In [6]:

varset = {"Console","Python","data","Flare"}
varset.add("Science")
print(varset)
{'data', 'Flare', 'Science', 'Python', 'Console'}

Sets do not allow duplicate values :

Sets remove duplicate values and keeps only one value in set.For Example :

In [7]:

varset = {"Console","Python","data","Flare","Flare","Flare","Flare"}
print(varset)
{'Python', 'data', 'Flare', 'Console'}

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]:

varset = {"Console","Python","data","Flare"}
max(varset)

In [10]:

varset = {"Console","Python","data","Flare"}
min(varset)

In [11]:

varset = {"Console","Python","data","Flare"}
len(varset)

Type of a set:

In [1]:

varset = {1,2,3}
type(varset)

Membership operator in set:

In [7]:

var_set = {1,2,3,4,5,6,7,8,9}
present = 1 in var_set
print(present)

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]:

var_set = {1,2,3,4,5,6,'console',7,8,9}
var_set.add("flare")
print(var_set)
{1, 2, 3, 4, 5, 6, 7, 8, 9, 'flare', 'console'}

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]:

var_set = {1,2,3,4,5,6,'console',7,8,9}
var_set.add(('Python','Data Science'))
print(var_set)
{1, 2, 3, 4, 5, 6, 7, 8, 9, 'console', ('Python', 'Data Science')}

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]:

var_set = {1,2,3,4,5,6,'console',7,8,9}
var_set.add([10,11])
print(var_set)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-20-ba1263667386> in <module>
      1 var_set = {1,2,3,4,5,6,'console',7,8,9}
----> 2 var_set.add([10,11])
      3 print(var_set)

TypeError: unhashable type: 'list'

In [21]:

var_set = {1,2,3,4,5,6,'console',7,8,9}
var_set.add({10,11})
print(var_set)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-286db87b4ce1> in <module>
      1 var_set = {1,2,3,4,5,6,'console',7,8,9}
----> 2 var_set.add({10,11})
      3 print(var_set)

TypeError: unhashable type: 'set'

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]:

set_a = {1,2,3,4,5}
set_b = {6,7,8,9}
newset = set_a.union(set_b)
print(newset)
{1, 2, 3, 4, 5, 6, 7, 8, 9}

In [3]:

set_a = {1,2,3,4,5}
list_b = [6,7,8,9]
newset = set_a.union(list_b)
print(newset)
{1, 2, 3, 4, 5, 6, 7, 8, 9}

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]:

set_a = {1,2,3,4,5}
set_b = {6,7,8,9}
set_a.update(set_b)
print(set_a)
{1, 2, 3, 4, 5, 6, 7, 8, 9}

If i want to change set_b , i will write above code something like this.

In [6]:

set_a = {1,2,3,4,5}
set_b = {6,7,8,9}
set_b.update(set_a)
print(set_b)
{1, 2, 3, 4, 5, 6, 7, 8, 9}

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]:

set_a = {1,2,3,4,5}
set_a.remove(5)
print(set_a)

Note:remove method will throw an error if element to be removed does not exist in set.

In [9]:

set_a = {1,2,3,4,5}
set_a.remove(7)  #7 does not exist in set
print(set_a)
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-9-778c40fc87bb> in <module>
      1 set_a = {1,2,3,4,5}
----> 2 set_a.remove(7)  #7 does not exist in set
      3 print(set_a)

KeyError: 7

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]:

set_a = {1,2,3,4,5}
set_a.remove(5)
print(set_a)

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]:

set_a = {1,2,3,4,5}
set_a.discard(7)  #7 does not exist in set
print(set_a)

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]:

set_a = {1,2,3,4,5}
set_a.pop()
print(set_a)

It also returns the deleted value from set. For Example:

In [13]:

set_a = {1,2,3,4,5}
item = set_a.pop()
print(set_a)
print("deleted item is",item)
{2, 3, 4, 5}
deleted item is 1

difference() and difference_update():

difference() method returns the uncommon values in the set. for example:

In [14]:

set_a = {1,2,3,4,5,6}
set_b = {4,5,6,7,8,9}
difference_set = set_a.difference(set_b)  #uncommon values of set_a is returned.
print(difference_set)

In [15]:

set_a = {1,2,3,4,5,6}
set_b = {4,5,6,7,8,9}
difference_set = set_b.difference(set_a)  #uncommon values of set_b is returned.
print(difference_set)

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]:

set_a = {1,2,3,4,5,6}
set_b = {4,5,6,7,8,9}
set_a.difference_update(set_b)
print(set_a)

In [18]:

set_a = {1,2,3,4,5,6}
set_b = {4,5,6,7,8,9}
set_b.difference_update(set_a)
print(set_b)

intersection() and intersection_update():

intersection() returns common values from two sets. For Example:

In [19]:

set_a = {1,2,3,4,5,6}
set_b = {4,5,6,7,8,9}
common = set_a.intersection(set_b)
print(common)

intersection_update():

intersection_update removes uncommon values from the set. It does not return any value . For example:

In [20]:

set_a = {1,2,3,4,5,6}
set_b = {4,5,6,7,8,9}
set_a.intersection_update(set_b)
print(set_a)

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]:

set_a = {1,2,3,4,5,6}
set_b = {4,5,6,7,8,9}
newset = set_a.symmetric_difference(set_b)
print(newset)

symmetric_difference_update :

This method changes the value of first set. where values are uncommon values from both sets. For example:

In [22]:

set_a = {1,2,3,4,5,6}
set_b = {4,5,6,7,8,9}
set_a.symmetric_difference_update(set_b)
print(set_a)

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]:

set_b = {1,2,3,4,5,6}
set_a = {4,5,6}
sub = set_a.issubset(set_b)
print(sub)

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]:

set_b = {1,2,3,4,5,6}
set_a = {4,5,6}
sup = set_b.issuperset(set_a)
print(sup)

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]:

set_b = {1,2,3,4,5,6}
set_a = {7,8,9}
output = set_a.isdisjoint(set_b)
print(output)

In [26]:

set_b = {1,2,3,4,5,6}
set_a = {1,7,8,9}
output = set_a.isdisjoint(set_b)
print(output)

clear():

To clear all the contents of set , we use clear() method.

In [27]:

set_b = {1,2,3,4,5,6}
set_b.clear()
print(set_b)

copy() :

To copy all the contents of a set in another set, we use copy method. For Example:

In [28]:

set_a = {1,2,3,4,5,6}
newset = set_a.copy()
print(newset)

set() Constructor :

set() constructor returns an iterable into set. For Example:

In [29]:

name = "Console Flare"
name = set(name)
print(name)
{'C', 'o', 'F', 'l', 's', ' ', 'a', 'e', 'n', 'r'}

In [30]:

name = [1,2,3]
name = set(name)
print(name)

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]:

empty_set = set()
print(empty_set)
set()

Previous9.Lists In PythonNext11.Tuples In Python

Last updated 11 months ago