WHAT ARE SETS IN PYTHON?

381 viewsPYTHONPython
0

WHAT ARE SETS IN PYTHON?

Saraswat World Changed status to publish June 16, 2023
0
SETS DEFINITION-

A set is basically a data structure consisting of a collection of any datatype unordered, unique, unchangeable, and unindexed elements, created by enclosing elements in curly braces or a set constructor.
Note-Set items are unchangeable, but you can remove and/or add items

HOW TO CREATE A SET IN PYTHON?

By using curly braces

Python Code

fruits = {"apple", "banana", "cherry"}
print(fruits) 
#output- {'cherry', 'banana', 'apple'}

By using the set() constructor

How to use the set() Constructor?

Python Code

fruits = set(("apple", "banana", "cherry")) #use constructor
print(fruits) 
#output- {'banana', 'cherry', 'apple'}

Duplicates are Not Allowed in set

Python Code

fruits = {"apple", "banana", "cherry", "apple"}
print(fruits) 
#output- {'banana', 'cherry', 'apple'}

True and 1 is considered the same valueย and

Set items can be of any data type

Python Code

aset = {"apple", "banana", "cherry", True, 1, 2}
print(asset) 
#output- {True, 2, 'banana', 'cherry', 'apple'}
HOW TO ACCESS SET ITEMS OF PYTHON SETS?

Set elements are unindexed. We cannot access items in a set by referring to an index or a key.

We can access set items using aย forย loop, or ask if a specified value is present in a set, by using theย inย keyword.

Usingย forย loop in sets

Python Code

aset = {"apple", "banana", "cherry"}
for x in aset:
   print(x)

Output

banana
apple
cherry

Usingย inย keyword

Python Code

fruits = {"apple", "banana", "cherry"}
print("banana" in fruits)

Output

True
HOW TO ADD ITEMS TO A SET IN PYTHON?

We can add one item to a set by using add() method as shown below-

Python Code

fruits = {"apple", "banana", "cherry"}
fruits.add("orange") #use add() method to add items
print(fruits)

Output

{'apple', 'cherry', 'banana', 'orange'}
HOW TO ADD SETS TO A SET IN PYTHON?

We can add items from another set into the current set by using the update() method as shown below-

Python Code

seta = {"apple", "banana", "cherry"}
setb = {"pineapple", "mango", "papaya"}
seta.update(setb)
print(seta)

Output


{'apple', 'mango', 'cherry', 'pineapple', 'banana', 'papaya'}
HOW TO ADD ITEMS OF ITERABLE OBJECTS TO A SET IN PYTHON?

We can add items of any iterable object (tuples, lists, dictionaries, etc.) to a set by using update() method as shown below-

Python Code

fruits = {"apple", "banana", "cherry"}
mylist = ["kiwi", "orange"]
fruits.update(mylist)
print(fruits)

Output

{'banana', 'cherry', 'apple', 'orange', 'kiwi'}
HOW TO REMOVE ITEM FROM SETS IN PYTHON?

To remove an item in a set, use the remove(), or the discard() method.

Usingย remove()ย Method in Python Sets

Python Code

thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)

Output

{'apple', 'cherry'}

Note: If the item to remove does not exist, remove() will raise an error.

Usingย discard()ย Method in Python Sets

Python Code

thisset = {"apple", "banana", "cherry"}
thisset.discard("banana")
print(thisset)

Output

{'apple', 'cherry'}

Note: If the item to remove does not exist, discard() will NOT raise an error.

Usingย pop()ย Method in Python Sets

We can remove any random item from a set using pop() method. The return value of pop() method is the removed item.

Python Code

fruits = {"apple", "banana", "cherry"}
x = fruits.pop()
print(x)
print(fruits)

Output

cherry
{'apple', 'banana'}

Usingย clear()ย Method in Python Sets

The clear() method empties the set

Python Code

fruits = {"apple", "banana", "cherry"}
fruits.clear()
print(fruits)

Output

set()

Usingย delย keyword in Python Sets

The del keyword will delete the set completely

Python Code

fruits = {"apple", "banana", "cherry"}
del fruits
print(fruits)

Output

NameError: name 'fruits' is not defined
LOOP ITEMS IN PYTHON SETS

Using aย forย loop

Python Code

fruits = {"apple", "banana", "cherry"}
for x in fruits:
  print(x)

Output

apple
cherry
banana
HOW TO JOIN SETS IN PYTHON?

Usingย union()ย Method in Python Sets

The union() method returns a new set with all items from both sets, it will exclude duplicate items.

Python Code

set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3)

Output

{'c', 'b', 3, 'a', 2, 1}

Usingย update()ย Method in Python Sets

The update() method inserts the items in set2 into set1, it will exclude duplicate items.

Python Code

set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set1.update(set2)
print(set1)

Output

{'a', 'b', 'c', 3, 2, 1}

Usingย intersection_update()ย Method in Python Sets

The intersection_update() method will keep only the items that are present in both sets.

Python Code

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.intersection_update(y)
print(x)

Output

{'apple'}

Usingย intersection()ย Method in Python Sets

The intersection() method will return a new set, that only contains the items that are present in both sets.

Python Code

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.intersection(y)
print(z)

Output

{'apple'}

Usingย symmetric_difference_update()ย Method in Python Sets

The symmetric_difference_update() method will keep only the elements that are NOT present in both sets.

Python Code

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.symmetric_difference_update(y)
print(x)

Output

{'google', 'banana', 'microsoft', 'cherry'}

Using theย symmetric_difference()ย Method in Python Sets

The symmetric_difference() method will return a new set, that contains only the elements that are NOT present in both sets.

Python Code

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.symmetric_difference(y)
print(z)

Output

{'google', 'banana', 'microsoft', 'cherry'}
SET METHODSย IN PYTHON

copy()ย Method in Python Sets

The copy() method copies the set.

Python Code

fruits = {"apple", "banana", "cherry"}
x = fruits.copy()
print(x)

Output

{'cherry', 'banana', 'apple'}

difference()ย Method in Python Sets

The difference() method returns a new set that contains the difference between two sets. The returned set contains items that exist only in the first set, and not in both sets.

Return a set that contains the items that only exist in set x, and not in set y

Python Code

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.difference(y)
print(z)

Output

{'cherry', 'banana'}

difference_update()ย Method in Python Sets

The difference_update() method removes the items from the original set that exist in both sets.

Python Code

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}

x.difference_update(y)

print(x)

Output

{'cherry', 'banana'}
WHAT IS THE DIFFERENCE BETWEEN DIFFERENCE() AND DIFFERENCE_UPDATE() METHOD OF PYTHON SETS?
DIFFERENCE() VS DIFFERENCE_UPDATE() METHOD

Theย difference()ย method returns aย new set, without the unwanted items, and theย difference_update()ย method removes the unwanted items from theย original set.

intersection()ย Method in Python Sets

Return a set that contains the items that exist in both sets (for example- set x, and set y as shown below) or in all sets if the comparison is done with more than two sets.

Syntax- set.intersection(set1, set2 โ€ฆ etc)

Python Code

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.intersection(y)
print(z)

Output

{'apple'}

Compare 3 sets, and return a set with items that is present in all 3 sets

Python Code

x = {"a", "b", "c"}
y = {"c", "d", "e"}
z = {"f", "g", "c"}
result = x.intersection(y, z)
print(result)

Output

{'c'}

intersection_update()ย Method in Python Sets

The intersection_update() method removes the items that is not present in both sets (or in all sets if the comparison is done between more than two sets).

Python Code

x = {"a", "b", "c"}
y = {"c", "d", "e"}
z = {"f", "g", "c"}
x.intersection_update(y, z)
print(x)

Output

{'c'}

isdisjoint()ย Method in Python Sets

The isdisjoint() method returns True if none of the items are present in both sets, otherwise it returns False.

Python Code

x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.isdisjoint(y)
print(z)

Output

False

issubset()ย Method in Python Sets

Return True if all items in set x are present in set y

Python Code

x = {"a", "b", "c"}
y = {"f", "e", "d", "c", "b", "a"}
z = x.issubset(y)
print(z)

Output

True

issuperset()ย Method in Python Sets

The issuperset() method returns True if all items in the specified set exists in the original set, otherwise it returns False. As shown in below example it returns True if all items of set y are present in set x.

Python Code

x = {"f", "e", "d", "c", "b", "a"}
y = {"a", "b", "c"}
z = x.issuperset(y)
print(z)

Output

True
Saraswat World Changed status to publish June 16, 2023
Write your answer.

Posted

in

Tags:

Comments

Leave a Reply

Ganesh Ji Arti A Tribute to Dilip Kumar