Table of Contents
In this tutorial, we will learn about the Python Dictionary pop() method with the help of examples.
The pop()
method removes and returns an element from a dictionary having the given key.
Example
# create a dictionary marks = { 'Physics': 67, 'Chemistry': 72, 'Math': 89 } element = marks.pop('Chemistry') print('Popped Marks:', element) # Output: Popped Marks: 72
1. Syntax of Dictionary pop()
The syntax of pop()
method is
dictionary.pop(key[, default])
2. pop() Parameters
pop()
method takes two parameters:
- key – key which is to be searched for removal
- default – value which is to be returned when the key is not in the dictionary
3. Return value from pop()
The pop()
method returns:
- If
key
is found – removed/popped element from the dictionary - If
key
is not found – value specified as the second argument (default) - If
key
is not found and default argument is not specified –KeyError
exception is raised
4. Example 1: Pop an element from the dictionary
# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 } element = sales.pop('apple') print('The popped element is:', element) print('The dictionary is:', sales)
Output
The popped element is: 2 The dictionary is: {'orange': 3, 'grapes': 4}
5. Example 2: Pop an element not present from the dictionary
# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 } element = sales.pop('guava')
Output
KeyError: 'guava'
6. Example 3: Pop an element not present from the dictionary, provided a default value
# random sales dictionary sales = { 'apple': 2, 'orange': 3, 'grapes': 4 } element = sales.pop('guava', 'banana') print('The popped element is:', element) print('The dictionary is:', sales)
Output
The popped element is: banana The dictionary is: {'orange': 3, 'apple': 2, 'grapes': 4}
Related posts:
Python Strings
Python Set discard()
Python Set isdisjoint()
How to get current date and time in Python?
Python Deep Learning - Valentino Zocca & Gianmario Spacagna & Daniel Slater & Peter Roelants
Machine Learning Applications Using Python - Cases studies form Healthcare, Retail, and Finance - Pu...
Applied Text Analysis with Python - Benjamin Benfort & Rebecca Bibro & Tony Ojeda
Natural Language Processing with Python - Steven Bird & Ewan Klein & Edward Loper
Python Artificial Intelligence Project for Beginners - Joshua Eckroth
Python Program to Make a Simple Calculator
Python Program to Count the Number of Digits Present In a Number
Python enumerate()
Python Machine Learning - Sebastian Raschka
Python classmethod()
Python Set pop()
Python Directory and Files Management
Python Program to Convert Two Lists Into a Dictionary
Python Set union()
Python Program to Check Leap Year
Python Program to Make a Flattened List from Nested List
Python hash()
Python Program to Count the Number of Occurrence of a Character in String
Deep Learning with Python - Francois Cholletf
Python pass statement
Machine Learning Mastery with Python - Understand your data, create accurate models and work project...
Python reversed()
Python Program to Slice Lists
Python compile()
Python Program to Differentiate Between type() and isinstance()
Python bool()
Python Program to Print all Prime Numbers in an Interval
Python Program to Return Multiple Values From a Function