Skip to main content

Command Palette

Search for a command to run...

Day34-Dictionary Methods

Updated
3 min read
Day34-Dictionary Methods
C
I am Data Science student, has a little bit knowledge on Web Development. I also love writing and editing as my hobby. Passionate to explore the world.

Dictionary Methods

Dictionary uses several built-in methods for manipulation. They are listed below:

i. update()

The update() method updates the value of the key provided to it if the item already exists in the dictionary, else it creates a new key-value pair.

Example:

info = {'name':'Karan', 'age':19, 'eligible':True}
print(info)
info.update({'age':20})
info.update({'DOB':2001})
print(info)

Output:

{'name': 'Karan', 'age': 19, 'eligible': True}
{'name': 'Karan', 'age': 20, 'eligible': True, 'DOB': 2001}

Removing items from dictionary:

There are a few methods that we can use to remove items from dictionary.

ii. clear():

The clear() method removes all the items from the list.

Example:

info = {'name':'Karan', 'age':19, 'eligible':True}
info.clear()
print(info)

Output:

{}

iii. pop():

The pop() method removes the key-value pair whose key is passed as a parameter.

Example:

info = {'name':'Karan', 'age':19, 'eligible':True}
info.pop('eligible')
print(info)

Output:

{'name': 'Karan', 'age': 19}

iv. popitem():

The popitem() method removes the last key-value pair from the dictionary.

Example:

info = {'name':'Karan', 'age':19, 'eligible':True, 'DOB':2003}
info.popitem()
print(info)

Output:

{'name': 'Karan', 'age': 19, 'eligible': True}

v. del:

we can also use the del keyword to remove a dictionary item.

Example:

info = {'name':'Karan', 'age':19, 'eligible':True, 'DOB':2003}
del info['age']
print(info)

Output:

{'name': 'Karan', 'eligible': True, 'DOB': 2003}

If key is not provided, then the del keyword will delete the dictionary entirely.

Example:

info = {'name':'Karan', 'age':19, 'eligible':True, 'DOB':2003}
del info
print(info)

Output:

NameError: name 'info' is not defined

Practice

ep1 = {122: 45, 123: 89, 567: 69, 670: 69}
ep2 = {222: 67, 566: 90}

# ep1.update(ep2)
# ep1.clear()
# ep1.pop(122)
ep1.popitem()
del ep1[122]
print(ep1) 

Output:

{123: 89, 567: 69}

Explanation

  • ep1 = {122: 45, 123: 89, 567: 69, 670: 69}

    • Creates a dictionary with employee IDs as keys and values as data.
  • ep2 = {222: 67, 566: 90}

    • Another dictionary (not used in final operations because update() is commented out).
  • ep1.update(ep2) (commented)

    • Would add all key-value pairs from ep2 into ep1.
  • ep1.clear() (commented)

    • Would remove all elements from ep1.
  • ep1.pop(122) (commented)

    • Would remove key 122 and return its value (45).
  • ep1.popitem()

    • Removes the last inserted key-value pair from the dictionary.

    • After this, one item is deleted from ep1.

  • del ep1[122]

    • Deletes the key 122 and its value from the dictionary.

    • ⚠️ Important: This will only work if key 122 still exists after popitem().

  • print(ep1)

    • Displays the final dictionary after deletions.

100DaysofPython

Part 35 of 35

This series is for beginners in which we explore python language along with how it is used in data science and do some exercises and some python related projects.

Start from the beginning

Day01- Introduction to Python

Python: Introduction, Features and Uses