×

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

Python Dictionary setdefault() method

Python Dictionary setdefault() method The dictionary.setdefault () method in Python returns the value of the item with the specified key. Syntax dictionary.setdefault(keyname, value) Parameter keyname- This parameter represents the keyname of the item you want...

1 minute read.

Python String isspace() method

Python String isspace() method The string.isspace() method returns a Boolean value true if there are only whitespace characters in the given string. This function is used to check if the given...

2 minutes read.

Returning Multiple Values in Python

Python is considered a general-purpose programming language; it is a high-level programming language that is not much difficult and easier to learn. It is rich in libraries that can be...

3 minutes read.

wxPython Panel class

wxPython Panel class The Widgets which is shown in the frame of GUI window such as text box, buttons, static text etc. are put inside the panel class of the wxpython...

2 minutes read.

Face Recognition in Python

In this tutorial, we will understand what is face recognition and how it is achieved in python. Face recognition is of great utility in real-world scenarios. It is an extended step...

3 minutes read.

Python String rjust() method

Python String rjust() method The string.rjust() method in Python returns a right-justified string of a given minimum width where the padding is done using the specified fillchar (default is a space). It returns...

2 minutes read.

Mrcnn Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

6 minutes read.

Python Multithreading

In python, we can implement multithreading. In this tutorial, we will understand the basics of implementing multithreading in a python programming language. Multithreading is just like multiprocessing. We use it...

4 minutes read.

How to plot multiple linear regression in Python

This article lets us look into linear regression in Python and how we can plot multiple linear regressions in it. Linear regression One of the simplest and most widely used Machine Learning...

4 minutes read.

Creating Web Application in python

Web Application :  Web Application is an application software that runs in web browser . Software programs will run on operating system. Whereas Web Applications will run on World Wide Web...

5 minutes read.

Difference between Sort and Sorted in Python

If you new to Python, it must be confusing the distinction between the Sort and Sorted functions. However, it is important to understand the differences in order to use them...

5 minutes read.

How to reverse a string in Python

In Python language, we have a few ways to reverse a string. In this article, let us discuss these ways and understand the suitability of these ways in different scenarios...

5 minutes read.

Abstraction in Python

What is meant by abstraction generally? A very general notion of a thing or work is known as abstract. To be more precise, the process of having a brief idea but...

4 minutes read.

How to Declare a Variable in Python?

The concept of constants and variables is something that we are studying right from our primary classes. We know that constants are the fixed values whereas variables are those whose...

4 minutes read.

Python Goto Statement

We all know that Python is the most basic and widely used programming language in the world. It is also one of the world's most popular and widely used languages....

4 minutes read.

Python program to check if two strings are anagram or not

Python program to check if two strings are anagram or not Problem: This is a python program that takes two strings and checks if given strings are anagram or not. Examples: Input: string1...

1 minute read.

Installing Packages in Python

It's critical to understand that the term "package" here refers to a collection of software that must be installed (i.e. as a synonym for a distribution). It has nothing to...

6 minutes read.

Python Data Visualization

In this tutorial, we will understand what data visualization means in python. Further, we will see different methods of visualizing data in python. Data visualization In a non-technical language, it is a...

4 minutes read.

os.stat() method in Python

In Python, the os module provides the capacity to interact with the operating system. The operating system comes under the Python module. In this module, Python provides a specific feature...

3 minutes read.

How to print in same line in python

How to Print in the Same Line in Python To print in Python, we use the print () function and the syntax of print () goes like this: print (values, sep =...

3 minutes read.