Set
- A set is a group of items where each item is unique.
- It is unordered (means there is no sequence and hence no index).
- Note: Ordered means there exists a sequence in the arrangements of grouped items. Unordered means there is no sequence.
- Ordered means that the order in which items were placed at the time of creation is maintained by the Python till no modification is done to the data itself. For ex: List and Tuple.
List Methods |
Set Methods |
append() |
add() |
extend() |
update() [Note: set does not support ‘+’ op.] |
remove() |
remove(), discard() |
pop(): removes the last element by default [Note: also accepts index to pop() an element] |
pop(): we cannot say which element will be popped as a set is not ordered. |
clear(): empty the list |
clear(): empty the set |
Intersection_update(): keeps the items in this set that are present in the other.
Now in code
Set is also a group of items in a single variable¶
Set is unordered , unindexed and can't have duplicate values¶
Set items are written in between curly brackets¶
set items are unordered, meaning: the items can appear in any order on display¶
set items are unchangeable in place ( because no index is associated with them to access )¶
We can add or remove items to and from set¶
Set can have items of mixed data type i.e - string, number , boolean and other object as well )¶
Given our knowledge about lists and tuples, let's see what we can do with sets already:¶
In [5]:
l = ['i', 'like', 'programming']
t = ('i', 'like', 'programming')
s = {'i', 'like', 'programming'}
In [6]:
print(type(l))
print(type(t))
print(type(s))
<class 'list'> <class 'tuple'> <class 'set'>
In [1]:
thisset = {"apple", "banana", "cherry", "apple"}
print(thisset)
{'banana', 'apple', 'cherry'}
A set is iterable.¶
In [2]:
for i in thisset:
print(i)
banana apple cherry
Creating set from a list¶
In [12]:
l = ["apple", "banana", "cherry", "apple"]
s = set(l) # Using type casting
print(s)
{'cherry', 'apple', 'banana'}
Creating a list from a set¶
In [5]:
s = {'cherry', 'apple', 'banana'}
l = list(s)
print(l)
#print(l[0:2])
print(list(s)[0:2])
['banana', 'apple', 'cherry'] ['banana', 'apple']
In [ ]:
In [15]:
print("guava" in s)
False
In [16]:
print("apple" in s)
True
How to remove duplicate values from a list?¶
In [2]:
x = input("enter the sentence: ").split()
print(x)
z = []
y = [z.append(i) for i in x if i not in z ]
print(z)
['i', 'like', 'like', 'like', 'programming'] ['i', 'like', 'programming']
In [3]:
words = ['i', 'like', 'like', 'like', 'programming']
In [4]:
set(words)
Out[4]:
{'i', 'like', 'programming'}
In [1]:
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.update(y)
print(x)
{'apple', 'google', 'cherry', 'microsoft', 'banana'}
In [ ]:
Now in detail
In [80]:
# Creating a set
thisset = {"apple", "banana", "cherry", "guava"}
print(thisset)
{'banana', 'apple', 'cherry', 'guava'}
In [2]:
# Creating set using set constructor
thisset = set(("apple", "banana", "cherry")) # note the double round-brackets
print(thisset)
{'banana', 'apple', 'cherry'}
In [3]:
# Type of Set
thisset = {"apple", "banana", "cherry"}
print(type(thisset))
<class 'set'>
In [1]:
# Set can't have duplicate values
set1 = {"apple", "banana", "cherry","apple"}
print(set1)
set2 = {True,2,3,False,0,"Hello",1}
print(set2)
{'cherry', 'apple', 'banana'} {False, True, 2, 3, 'Hello'}
In [6]:
# Set Items are unindexed
thisset = {"apple", "banana", "cherry","mango","orange","papaya"}
print(thisset[0])
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[6], line 4 1 # Items appear in random order 3 thisset = {"apple", "banana", "cherry","mango","orange","papaya"} ----> 4 print(thisset[0]) TypeError: 'set' object is not subscriptable
In [12]:
# Set can have items of any type including mixed one
set1 = {"apple", "banana", "cherry"}
set2 = {1, 5, 7, 9, 3}
set3 = {True, False, False}
set4 = {True,2,3,False,0,"Hello",1}
print(set1) # string type itmes
print(set2) # number type itmes
print(set3) # boolean type itmes
print(set4) # mixed type itmes , note ignored duplicate values
{'banana', 'cherry', 'apple'} {1, 3, 5, 7, 9} {False, True} {False, True, 2, 3, 'Hello'}
In [8]:
# Add an item in a set
set1 = {True,False,"Hello",1} # Note mixed data types of items
print(set1) # True and 1 treated as duplicate values
set1.add("Ravi")
print(set1) # print after adding new item 'Ravi'
{False, True, 'Hello'} {False, True, 'Ravi', 'Hello'}
In [9]:
# Removing an item from set
set1 = {True,False,"Hello",1 , "Ravi"} # Note mixed data types of items
print(set1) # True and 1 treated as duplicate values
set1.remove("Ravi")
print(set1) # print after removing item 'Ravi'
{False, True, 'Ravi', 'Hello'} {False, True, 'Hello'}
In [8]:
# Removing an item from set using discard built in method
set1 = {True,False,"Hello",1 , "Ravi"} # Note mixed data types of items
print(set1) # True and 1 treated as duplicate values
set1.discard("Hello")
print(set1) # print after removing item 'Hello'
{False, True, 'Ravi', 'Hello'} {False, True, 'Ravi'}
In [9]:
# Difference between remove and discard method
# If item to be removed not present in set
# Then remove method raise an Error msg ( Key Error )
# But discard method not raise any error message
set1 = {True,False,"Hello",1 , "Ravi"} # Note mixed data types of items
print(set1) # True and 1 treated as duplicate values
set1.discard("Orange") # Note not present in set
print(set1)
set1.remove("Orange") # Note not present in set
print(set1)
{False, True, 'Ravi', 'Hello'} {False, True, 'Ravi', 'Hello'}
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Cell In[9], line 9 7 set1.discard("Orange") # Note not present in set 8 print(set1) ----> 9 set1.remove("Orange") # Note not present in set 10 print(set1) KeyError: 'Orange'
In [84]:
# Removing last item from set using pop builtIn Method
set1 = {"Tail","Head","Up","Top" , "Down"}
print(set1)
ItemRemoved = set1.pop() # No index passed as an argument
print(set1) # print after removing last item , note no order exist for items
print("Removed Item is : " + str(ItemRemoved) )
{'Down', 'Up', 'Top', 'Tail', 'Head'} {'Up', 'Top', 'Tail', 'Head'} Removed Item is : Down
In [93]:
# Removing last item from set using pop builtIn Method
set1 = {"Tail", "Head", "Up", "Top", "Down"}
set2 = set({}) # {} is not an empty set, it is dictionary.
for i in range(len(set1)):
item_removed = set1.pop()
set2.add(item_removed)
In [94]:
set2
Out[94]:
{'Down', 'Head', 'Tail', 'Top', 'Up'}
In [92]:
x = {}
print(type(x))
<class 'dict'>
In [14]:
# length of set
thisset = {"apple", "banana", "cherry", "mango", "orange", "papaya", "apple"}
print(len(thisset)) # Note ignore duplicate items while counting the items
6
In [15]:
# Accessing set items
# We cannot access items in a set by referring to an index or a key
# Iterating Set items using for loop and membership operator 'in'
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
banana cherry apple
In [2]:
# Check if an item prsent or not in a set
thisset = {"apple", "banana", "cherry"}
print("banana" in thisset)
print("orange" in thisset)
True False
In [2]:
# To add set items in another set using built in update method
SetOne = {"apple", "banana", "cherry"}
SetTwo = {"pineapple", "mango", "papaya"}
SetOne.update(SetTwo)
print(SetOne)
{'mango', 'apple', 'cherry', 'papaya', 'pineapple', 'banana'}
In [3]:
SetOne = {"apple", "banana", "cherry"}
SetTwo = {"pineapple", "mango", "papaya"}
SetThree = SetOne + SetTwo
print(SetThree)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) /tmp/ipykernel_47615/2785294167.py in <module> 1 SetOne = {"apple", "banana", "cherry"} 2 SetTwo = {"pineapple", "mango", "papaya"} ----> 3 SetThree = SetOne + SetTwo 4 print(SetThree) TypeError: unsupported operand type(s) for +: 'set' and 'set'
In [4]:
# Adding items of any iterable object in a set
# Iterable object like : tupple , range , list
# For Ex - Adding a list items
Set = {"apple", "banana", "cherry"}
List = ["kiwi", "orange"]
Set.update(List)
print(Set)
{'kiwi', 'orange', 'banana', 'cherry', 'apple'}
In [5]:
# For Ex - Adding a tuple items
Set = {"apple", "banana", "cherry"}
Tuple = [1,2,3,"Orange",False] # Mixted type items
Set.update(Tuple)
print(Set)
{False, 1, 2, 3, 'Orange', 'banana', 'cherry', 'apple'}
In [7]:
# For Ex - Adding a range items
Set = {"apple", "banana", "cherry"}
Range = range(0,6)
Set.update(Range)
print(Set)
{0, 1, 2, 3, 4, 5, 'banana', 'cherry', 'apple'}
In [18]:
# Empty the set
thisset = {"apple", "banana", "cherry"}
thisset.clear()
print(thisset)
set()
In [21]:
# Deleting set using del method
thisset = {"apple", "banana", "cherry"}
del thisset
print(thisset) # Try to print set items after deleting set
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[21], line 5 3 thisset = {"apple", "banana", "cherry"} 4 del thisset ----> 5 print(thisset) NameError: name 'thisset' is not defined
In [23]:
# Joining sets using union method
# This will return a new set
set1 = {"a", "b" , "c" , 1, 4 , 5}
set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3) # Note all duplicate values ignored
{'b', 1, 2, 3, 4, 5, 'a', 'c'}
In [31]:
# Updating a set by adding items of another set
set1 = {"a", "b" , "c" , 1, 4 , 5}
set2 = {1, 2, 3}
print(set1)
set1.update(set2)
print(set1) # Note all duplicate values ignored
{'b', 1, 'c', 4, 5, 'a'} {'b', 1, 2, 3, 4, 5, 'a', 'c'}
In [30]:
# Updating a set to have only duplicate values using intersection_update method
# This will return a new set
set1 = {"a", "b" , "c" , 1, 4 , 5}
set2 = {1, 2, 3}
print(set1)
set1.intersection_update(set2) # Elements from set1 that are also in set2.
print(set1) # Items of set1 after update
{'b', 1, 'c', 4, 5, 'a'} {1}
In [26]:
# Getting a new set of duplicate items of two sets using intersection method
set1 = {"a", "b" , "c" , 1, 4 , 5}
set2 = {1, 2, 3 , "b"}
set3 = set1.intersection(set2)
print(set3)
{'b', 1}
In [29]:
# Geting a new set of all unique items of two sets using symmetric_difference method
set1 = {"a", "b" , "c" , 1, 4 , 5}
set2 = {1, 2, 3 , "b"}
set3 = set1.symmetric_difference(set2)
print(set3)
{2, 3, 'c', 4, 5, 'a'}
In [32]:
# Updating a set to have all unique items using symmetric_difference_update method
set1 = {"a", "b" , "c" , 1, 4 , 5}
set2 = {1, 2, 3 , "b"}
print(set1)
set1.symmetric_difference_update(set2)
print(set1)
{'b', 1, 'c', 4, 5, 'a'} {2, 'c', 4, 5, 3, 'a'}
In [33]:
# Updating a set to have values which are present in the set using difference_update method
set1 = {"a", "b" , "c" , 1, 2 , 3}
set2 = {"a", "b" , "c" , "d"}
print(set1)
set1.difference_update(set2)
print(set1) # Note 'd' not included in resultant set
{'b', 1, 2, 3, 'c', 'a'} {1, 2, 3}
In [97]:
set1 = {"a", "b" , "c" , 1, 2 , 3}
set2 = {"a", "b" , "c" , "d"}
print("Difference")
print(set1 - set2)
set1 = {"a", "b" , "c" , 1, 4 , 5}
set2 = {1, 2, 3 , "b"}
set3 = set1.difference(set2)
print(set3)
set4 = set2.difference(set1)
print(set4)
print("Symmetric Difference")
print(set3.union(set4))
Difference {1, 2, 3} {'c', 'a', 4, 5} {2, 3} Symmetric Difference {2, 3, 4, 5, 'a', 'c'}
In [34]:
# Geting a new set to have values which are only present in the first set using difference method
set1 = {"a", "b" , "c" , 1, 2 , 3}
set2 = {"a", "b" , "c" , "d"}
set3 = set1.difference(set2)
print(set3) # Note 'd' not included in resultant set
{1, 2, 3}
In [ ]:
# Built In methods associated with set data type to perform various type operations
"""
Method Description
add() Adds an element to the set
clear() Removes all the elements from the set
copy() Returns a copy of the set
difference() Returns a set containing the difference between two or more sets
difference_update() Removes the items in this set that are also included in another, specified set
discard() Remove the specified item
intersection() Returns a set, that is the intersection of two other sets
intersection_update() Removes the items in this set that are not present in other, specified set(s)
isdisjoint() Returns whether two sets have a intersection or not
issubset() Returns whether another set contains this set or not
issuperset() Returns whether this set contains another set or not
pop() Removes an element from the set
remove() Removes the specified element
symmetric_difference() Returns a set with the symmetric differences of two sets
symmetric_difference_update() inserts the symmetric differences from this set and another
union() Return a set containing the union of sets
update() Update the set with the union of this set and others
"""