9 Sets, Dictionary 28 11 2022
9 Sets, Dictionary 28 11 2022
SETS
• A set is a collection which
is unordered, unchangeable*,
and unindexed.
• Sets are written with curly brackets.
Create a Set:
thisset ={"apple", "banana", "cherry"}
print(thisset)
Set Items
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)
update() Update the set with the union of this set and others
Get the Length of a Set
• To determine how many items a set has,
use the len() function.
• Example
• Get the number of items in a set:
• thisset = {"apple", "banana", "cherry"}
• print(len(thisset))
type()
• What is the data type of a set?
myset
={"apple", "banana", "cherry"}
print(type(myset))
Add Items
thisset.discard("banana")
print(thisset)
Note : If the item to remove does not exist, discard() will NOT raise an error.
You can also use the pop() method to remove an item,
but this method will remove the last item.
Remember that sets are unordered,
so you will not know what item that gets removed.
The return value of the pop() method is the removed item.
x = thisset.pop()
print(x)
print(thisset)
copy()
The copy() method copies the set.
x = fruits.copy()
Parameter Values
No parameters print(x)
Example:
isdisjoint() Method
Definition and Usage
The isdisjoint() method returns True if none of
the items are present in both sets, otherwise it
returns False.
Syntax
set.isdisjoint(set)
Parameter Values
set Required. The set to search for equal
items in
• Example
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.isdisjoint(y)
print(z)
issubset()
The issubset() method returns True if all
items in the set exists in the specified set,
otherwise it retuns False.
Syntax
set.issubset(set)
Parameter Values
set Required. The set to search for equal
items in
Example:
x = {"a", "b", "c"}
y = {"a", "b", "d", "c", "b"}
z = x.issubset(y)
print(z)
issuperset()
The issuperset() method returns True if all
items in the specified set exists in the
original set, otherwise it retuns False.
Syntax
set.issuperset(set)
Parameter Values
set Required. The set to search for equal
items in
Example
x = {"f", "e", "d", "c", "b"}
y = {"a", "b", "c"}
z = x.issuperset(y)
print(z)
Set clear() Method
The clear() method empties the set:
for x in thisset:
print(x)
Union()
The union() method returns a new set
with all items from both sets:
Syntax
set.union(set1, set2...)
set1.update(set2)
print(set1)
Syntax
set.intersection_update(set1, set2 ... etc)
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.intersection_update(y)
print(x)
Example
Return a set that contains all items from both sets, except items that are
present in both:
Syntax
set.symmetric_difference(set)
z = x.symmetric_difference(y)
print(z)
The symmetric_difference_update() method will keep
only the elements that are NOT present in both sets.
Example
x.symmetric_difference_update(y)
print(x)
set copy()
set1 = {1, 2, 3, 4}
# function to copy the set
set2 = set1.copy()
# prints the copied set
print(set2)