Python

pop() method in dictionary

Click for All Topics

pop() method in dictionary - Python

  • This method is used to remove an item from a dictionary based on the specified key.
  • It will throw an error KeyError if the specified key is not present in the dictionary and no default value is given.
  • It returns the value of the key that is removed from the dictionary.
  • The time complexity of this method is O(1) constant.
  • When we call the pop() method with a dictionary and pass a key as an argument, Python first calculates the hash value of the specified key and checks whether the specified index is pointing to the desired key-value pair in the hash table or not. If the key exists, Python removes the key-value pair from the hash table and returns the value associated with the removed key. If the key does not exist, a KeyError is raised to indicate that the specified key is not present in the dictionary.

Syntax –

dictionary.pop(key, default)

Parameters –

  • key → key of the item that you want to remove from the dictionary.
  • default(optional)→ default value that you want to return if the specified key is not found.
  • return → returns the value of the specified key if the key is found.

Example –

				
					my_dict={'a':5,'b':10,'c':15,'d':20,'e':25}
print("Original Dictionary:\n",my_dict)

my_dict.pop('d')
print("Original dictionary after removing 'd':20 from dictionary:\n",my_dict)

# Output -
# Original Dictionary:
#  {'a': 5, 'b': 10, 'c': 15, 'd': 20, 'e': 25}
# Original dictionary after removing 'd':20 from dictionary:
#  {'a': 5, 'b': 10, 'c': 15, 'e': 25}
				
			
  • In the above example, first we create a dictionary with some items and store it in my_dict variable.
  • After creating a dictionary print the dictionary on the output screen.
  • Then pop item(’d’:20) from the original dictionary using the .pop() method.
  • Finally, print the modified original dictionary on the output screen.

Example –

				
					student={'name':'anurag','year':4,'rollno':1234,'branch':'cse'}
print("Original Dictionary:",student)

return_value=student.pop('year')
print('Original Modified dictionary:",student)
print('year of the student:',return_value)

# Output -

# Original Dictionary: {'name': 'anurag', 'year': 4, 'rollno': 1234, 'branch': 'cse'}
# Original Modified dictionary: {'name': 'anurag', 'rollno': 1234, 'branch': 'cse'}
# year of the student: 4
				
			

Example – pop with the default value

				
					course={1:'Data Science',2:'Full Stack',3:'Devops',4:'Cyber Security'}
print("Available Courses:\n",course)

return_course=course.pop(5,'course not found')
print("Modified Courses:\n",course)
print("return value:",return_course)

# Output -
# Available Courses: 
# {1: 'Data Science', 2: 'Full Stack', 3: 'Devops', 4: 'Cyber Security'}
# Modified Courses: 
# {1: 'Data Science', 2: 'Full Stack', 3: 'Devops', 4: 'Cyber Security'}
# return value: course not found
				
			

Example – pop without a default value and the item is not present

				
					vacancy={'HR':1,'developer':3,'content writer':2,'data analyst':4}
print("Vacancy Available:\n",vacancy)

vacancy.pop('project manager')

# Output -
# Vacancy Available:
# {'HR': 1, 'developer': 3, 'content writer': 2, 'data analyst': 4}
# Traceback (most recent call last):
  # File "/home/main.py", line 4, in <module>
    # vacancy.pop('project manager')
# KeyError: 'project manager'