Remove Items from Set in Python? (Pop, Remove, Discard, Clear, Del)

https://www.youtube.com/watch?v=bnF65r5HqTM

🎓 Welcome back to Digital Academy, the Complete Python Development Tutorial for Beginners, which will help you Learn Python from A to Z!

🖥️ Remove Items from Set in Python? (Pop, Remove, Discard, Clear, Del)

There are several ways to remove items from a Set in Python: either by its value, a random item within your set – and even remove all of them, at once.

○ Remove an Item by Index (Pop)

If you do not know the value of the item you want, you can use the pop() method to remove an item, located at a random position. It will: modify the set and returns the removed item.

my_set = {'red', 'green', 'blue'}
my_item = my_set.pop()
# my_set = {'green', 'red'}

# Removed item
print(my_item) # Blue



○ Remove an Item by Value (Remove, Discard)

If you are not sure where the item is in the set and that you want to remove a specific and single value but do not need to store it, you can use the remove() or discard() methods to delete this item by its value. But, keep in mind that: if more than one instance of the given item is present, this method removes only the first instance.

Both methods remove() and discard() work exactly the same. The only difference is that: If the specified item is not present in a set: the method remove() raises KeyError, whereas the method discard() does nothing.

○ Method remove()

my_set = {'red', 'green', 'blue'}
my_set.remove('red')
# my_set = {'green', 'blue'}


○ Method discard()

my_set = {'red', 'green', 'blue'}
my_set.discard('red')
# my_set = {'green', 'blue'}



○ Remove ALL Items from a Set in Python (Clear)

You can also use the clear() method to permanently and fully remove ALL items from the set in Python, at once. And the specific del keyword to delete the set itself, entirely.

my_set = {'red', 'green', 'blue'}
my_set.clear()
# my_set = set()


Let’s play this video, stick around and watch until the end of this video! 👍🏻

– Digital Academy™ 🎓

***

☞ WATCH NEXT:
○ Data Types in Python – https://youtu.be/cweUByxBWiU
○ Operators in Python – https://youtu.be/-wDaVLkKOiU
○ IF Statements in Python – https://youtu.be/CC5seZ6OBJ4
○ FOR Loops in Python – https://youtu.be/JgH-D5DSTho

☞ WATCH MORE:
○ HOW TO Learn Python? Python Tutorial for Beginners [FULL Course] https://youtu.be/9hvnSZPMtuw

📖 Blog: http://digital.academy.free.fr/blog/

📖 Complete Python Development Course for Beginners [PLAYLIST]: http://digital.academy.free.fr/playlist/python-development-for-beginners

🛒 Shopping and Discounts: http://digital.academy.free.fr/store/

#Python #Tutorial #Beginners #Shorts

***

♡ Thanks for watching and supporting ♡
Please Subscribe. Hit the notification bell.
Like, Comment and Share.

***

♡ FOLLOW US ♡
http://digital.academy.free.fr/
https://twitter.com/DigitalAcademyy
https://www.facebook.com/digitalacademyfr
https://www.instagram.com/digital_academy_fr/
https://www.youtube.com/c/DigitalAcademyOnline

♡ SUPPORT US ♡
http://digital.academy.free.fr/join
http://digital.academy.free.fr/donate
http://digital.academy.free.fr/subscribe
https://www.patreon.com/digital_academy
https://www.buymeacoffee.com/digital_academy

***

Copy link
Powered by Social Snap