×

Python Dictionary pop() method

Python Dictionary pop() method

The dictionary.pop() method in removes the specified item and returns an element from a dictionary having the given key.

Syntax

dictionary.pop(key[, default])

Parameter

key – This argument signifies the key which is to be searched for removal.

default – This parameter represents the value which is to be returned when the key is not in the dictionary.

Return

This method removes and returns an element from a dictionary if the key is found else a KeyError exception is raised if the key if not found and the default argument is not specified.

Example 1

 # Python program explaining
 # the dictionary.pop() method
 # initialising the dictionary
 fruits = {
  "banana" : "apple",
   "orange": "mango",
   "grapes": 5
 }
 # printing the Dictionary
 print("Dictionary:",fruits)
 # removing the "banana" element from the dictionary.
 leftFruit=fruits.pop("banana")
 print("My final lists of fruits are\n", fruits) 

Output

 Dictionary: {'banana': 'apple', 'grapes': 5, 'orange': 'mango'}
 My final lists of fruits are
  {'grapes': 5, 'orange': 'mango'} 

Example 2

 # Python program explaining
 # the dictionary.pop() method
 # initializing dictionary  
 dictionary = { "Saturday" : 6, "Friday" : 5, "Monday" : 1,"Tuesday": 2 } 
 # Printing the specified dictionary 
 print ("The dictionary before deletion : " + str(dictionary)) 
 # using pop to return and remove key-value pair 
 # provided default 
 popValue = dictionary.pop('Friday', 5) 
 # Printing the value co-linked with the specified popped key  
 print ("Value linked with poppped key is : " + str(popValue)) 
 # using pop to return and remove key-value pair 
 # not provided default 
 popValue = dictionary.pop('Friday') 
 # Printing the value associated to popped key  
 # KeyError 
 print ("Value linked to poppped key is : " + str(popValue)) 

Output

 The dictionary before deletion : {'Tuesday': 2, 'Monday': 1, 'Saturday': 6, 'Friday': 5}
 Value linked with poppped key is : 5
 Traceback (most recent call last):
   File "main.py", line 19, in <module>
     popValue = dictionary.pop('Friday') 
 KeyError: 'Friday' 

Example 3

 # Python program explaining
 # the dictionary.pop() method
 # initializing the fruits dictionary
 fruits = { 'apple': 2, 'orange': 3, 'grapes': 4 }
 # Poping an element that is not present from the dictionary, provided a default value
 popList = fruits.pop('pineapple', 'strawberry')
 print('The popped element is:', popList)
 print('The dictionary is:', fruits) 

Output

The popped element is: strawberry
The dictionary is: {'grapes': 4, 'apple': 2, 'orange': 3}

Related Topics

How to build a Virtual Assistant Using Python

What is a virtual assistant? A virtual assistant is a new and very interesting concept in today’s world. When we hear the word “Virtual Assistant”, we can easily visualize “Jarvis or...

8 minutes read.

Standard GUI Unit Converter using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

12 minutes read.

How to open a file in python

Opening a File in Python Python is a user-friendly programming language that makes almost every concept easy. It provides many inbuilt functions in its libraries to work with files. Using these...

6 minutes read.

How to uninstall python

To un-install Python, we need to follow different procedures in different operating systems. In this article, we will discuss the un-installation process of Python in Windows, Mac, and Linux operating...

4 minutes read.

Python Lists vs Tuples

The difference between lists and tuples is one of the most frequently asked questions in an interview related to python language. Lists and Tuples are two of Python’s built-in data...

4 minutes read.

Python exe() function

Python exe() function The exec() function in Python executes the specified Python code and accepts large blocks of code. It supports dynamic execution of Python code where the object must be either a string...

1 minute read.

Python Pascal Triangle

Python Pascal Triangle Pascal triangle A pascal triangle is a number pattern of triangular array of the binomial coefficients. For designing a pascal triangle, we write a function in the program which...

5 minutes read.

Python String split() method

The string.split() method in Python splits a string into a list and returns a list of the words in the string. If the parameter maxsplit is given, at most maxsplit splits are done. If maxsplit is...

2 minutes read.

Accessing Key-value in Dictionary in Python

A Python word reference is an assortment of key-esteem matches where each key is related to worth. Worth in the key-esteem pair can be a number, a string, a rundown,...

5 minutes read.

Composition in Python

Composition in Python Composition is one of the important concepts of Object-oriented programming (OOPs). Composition basically enables us for creating complex types objects by combining other types of objects in the...

6 minutes read.

Defaultdict in Python

In this tutorial, we will study What is defaultdict in Python We will understand it with the aid of certain examples. Before this, we will have a look at dictionaries...

3 minutes read.

Rank Based Percentile GUI Calculator using PyQt5 in Python

PyQt5: PyQt5 is one of several solutions that Python offers for creating GUI applications. Cross-platform GUI toolkit PyQt5 is a collection of Python interfaces for Qt version 5. With the capabilities...

3 minutes read.

How to Import Files in Python

Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Shallow Copy and Deep Copy in Python

Shallow Copy and Deep Copy in Python In this section, we will learn about the Shallow Copy and Deep Copy in the Python program. But before going through the topic, we...

6 minutes read.

Cross Validation in Sklearn

Data scientists can benefit from cross-validation in machine learning in two key ways: it can assist in minimising the amount of data needed and ensure the artificial intelligence model is...

14 minutes read.

Compound Interest GUI Calculator using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

6 minutes read.

What is Collaborative Filtering in ML, Python

Introduction Contents recommendation is a useful tactic for almost any specific technology looking to increase interest, but it frequently calls for a lot of user data and perhaps laborious content tagging...

3 minutes read.

Alexa Python

The cloud-based voice assistant renders text into speech in a female voice. Alexa is a virtual assistant released by Amazon. Using itself as a system for home automation, Alexa can...

4 minutes read.

Python String center() method

Python String center() method The center() method will center align the string, using a specified character (space is default) as the fill character. Syntax string.center(width[, fillchar]) Parameter width This parameter represents the length of the returned string. fillchar...

1 minute read.

Python Argmin

Introduction The argmin function is defined as numpy.argmin(). This function returns the index of the minimum value or element from a Numpy array in a specific axis. An array is taken...

3 minutes read.