Remove Item from Dictionary in Python (Pop, Popitem, Del, Clear)

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

🖥️ Remove Item from Dictionary in Python (Pop, Popitem, Del, Clear)

There are several ways to remove items from a dictionary in Python: either by its key, the last inserted item, a random item, or even remove all of them at once.

○ Remove an Item by Key from Dictionary (Pop or Del)

If you know the key of the item you want: you can remove a particular item in a dictionary, by using the function pop(). This function removes an item with the provided key, and returns its value.

my_dict = {
    'age': 27,
    'job': 'Youtuber',
    'name': 'Digital Academy'
}
     
x = my_dict.pop('age')  # x = 27
# my_dict = {'job': 'Youtuber', 'name': 'Digital Academy'}


If you do not need to keep nor use the removed value, you can also use the del keyword to remove individual items. To delete an entry, use the del statement, specifying the key to delete.

my_dict = {
    'age': 27,
    'job': 'Youtuber',
    'name': 'Digital Academy'
}
     
del my_dict['age']
# my_dict = {'job': 'Youtuber', 'name': 'Digital Academy'}



○ Remove Last Inserted Item from Dictionary (Popitem)

Sometimes you need to iterate through a dictionary in Python and delete its items sequentially. To accomplish this task, you can use the function popitem(), which will remove and return the last inserted item from the dictionary. Please note that in Python versions before 3.7, the popitem() method removes a random item – not necessarily the last inserted!

my_dict = {
    'age': 27,
    'job': 'Youtuber',
    'name': 'Digital Academy'
}
     
name = my_dict.popitem() # ('name', 'Digital Academy')
# my_dict = {'age': 27, 'job': 'Youtuber'}



○ Remove all Items from Dictionary (Clear)

All the items can be removed at once, using the clear() method. Consequently, it will delete all keys and values from the dictionary. You can also use the del keyword, to remove the entire dictionary itself.

my_dict = {
    'age': 27,
    'job': 'Youtuber',
    'name': 'Digital Academy'
 }
     
 my_dict.clear()
 # my_dict = {}


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