How to add new items in python sets .pptx

AsimMukhtarCheema1 5 views 9 slides Sep 02, 2024
Slide 1
Slide 1 of 9
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9

About This Presentation

Learn how to add new elements to Python sets using the add() and update() methods. Discover practical tips and avoid common mistakes.

🔔 Subscribe to our YouTube channel for more Python tips and tutorials!


Slide Content

Removing python set items

Removing set items Set items can be removed by using remove () or discard () method. If the item to remove does not exist, remove() will raise an error.

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

Discard method example: thisset = { "apple" , "banana" , "cherry" } thisset.discard( "banana" ) print (thisset)

The pop() method: Pop () method will remove a random item, so you cannot be sure what item that gets removed. The return value of the pop() method is the removed item.

Example: thisset = { "apple" , "banana" , "cherry" } x = thisset.pop() print (x) print (thisset)

The clear() method The clear () method empties the set. thisset = { "apple" , "banana" , "cherry" } thisset.clear() print (thisset)

The del function method The del keyword will delete the set completely: thisset = { "apple" , "banana" , "cherry" } del thisset print (thisset)

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