×

Python Dictionary popitem() method

Python Dictionary popitem() method

The dictionary.popitem() method in Python removes the item that was last inserted into the dictionary.

Syntax

dictionary.popitem()

Parameter

NA

Return

This method returns an arbitrary element (key, value) pair from the given dictionary or raises a KeyError if the specified dictionary is null.

Example 1

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

Output

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

Example 2

 # Python program explaining
 # the dictionary.popitem() method
 #initializing the dictionary
 dictionary = {1: "numbers", 2: "digits", 3: "alphabets", 4:"Symbols",4:"Special characters"}
 print("Actual dictionary: ",dictionary)
 # pop the last element
 dictionary.popitem()
 print('Remaining dictionary: ', dictionary)
 # pop the last element
 dictionary.popitem()
 print('Remaining dictionary: ', dictionary)
 # pop the last element
 dictionary.popitem()
 print('Remaining dictionary: ', dictionary)
 # pop the last element
 dictionary.popitem()
 print('Remaining dictionary: ', dictionary)
 # pop the last element
 dictionary.popitem()
 print('Remaining dictionary: ', dictionary)
 # popping the item when the dictionary is empty
 # will return a keyerror
 dictionary.popitem()
 print('Remaining dictionary: ', dictionary) 

Output

 Actual dictionary:  {1: 'numbers', 2: 'digits', 3: 'alphabets', 4: 'Special characters'}
 Remaining dictionary:  {2: 'digits', 3: 'alphabets', 4: 'Special characters'}
 Remaining dictionary:  {3: 'alphabets', 4: 'Special characters'}
 Remaining dictionary:  {4: 'Special characters'}
 Remaining dictionary:  {}
 Traceback (most recent call last):
   File "main.py", line 23, in <module>
     dictionary.popitem()
 KeyError: 'popitem(): dictionary is empty' 

Related Topics

Python Logging Maxbytes

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....

6 minutes read.

Python Dictionary popitem() method

Python Dictionary popitem() method The dictionary.popitem() method in Python removes the item that was last inserted into the dictionary. Syntax dictionary.popitem() Parameter NA Return This method returns an arbitrary element (key, value) pair from the given dictionary...

1 minute read.

Python System Command

To execute a program in Python, we need to execute some shell commands to run our program on the computer. Python will provide some shell commands in our background to...

3 minutes read.

Python String lower() method

Python String lower() method The string.lower() method in Python returns a string where all characters are lower case. Syntax string.lower() Parameter NA Return This method returns a string where all characters are lower case. Example 1 # Python...

1 minute read.

What is the re.sub() function in Python

There. sub () function is used to return a string by replacing the occurrences of a specific character or pattern with a replacement string. To use this function, import the...

3 minutes read.

Python logging Module

Python logging Module Introduction By logging word we understand the tracking of the events which happens when we run some software. Logging process is very important part for developing software, debugging...

12 minutes read.

Method Overloading in Python

Overloading is a capability of a method and operator to behave differently according to the nature of parameters. Overloading is popular because it provides a good number of advantages: Reusability of code.Reduces...

3 minutes read.

Python Empty Tuple

How to Create an Empty Tuple Tuple A tuple is a data structure used to store non-homogeneous data elements. These non-homogeneous data elements consist of integer data type, character data type, String...

3 minutes read.

List Subtract in Python

The List is one of the most unique Data Structures in Python. It is generally used to store multiple values in just one single variable. It is one of the...

4 minutes read.

File Explorer using Tkinter in Python

File Explorer: Users can control folders and files on a device using an application known as a file manager or file explorer. Customers can access, edit, copy, delete, and start moving files...

3 minutes read.

Simple GUI calculator using PyQt5 in Python

GUI: The user is provided with information using manipulable visual widgets that don't require command-line input. These interface components respond to the user's interactions per the pre-programmed script, assisting each user's...

4 minutes read.

Socket Programming in Python

Socket Programming in Python In this tutorial, we will discuss network programming using Python programming language. We will explore all basic concept of network with Python script. Network Services in Python Python has...

9 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.

Algorithm for Factorial of a number in Python

What is a Factorial Number? In mathematics, the factorial of a positive integer n, denoted by n!. It is the product of all positive integers less than or equal to n. For...

3 minutes read.

Compound Interest GUI Calculator using PyQt5 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.

Flutter with tensor flow in python

Python : Python is an object oriented programming language which is highly interpreted and is highly interactive. Python was created by Guido van Rossum in the year 1985 – 1990 .The...

3 minutes read.

Python whois

What is whois? Whois is a protocol used to identify the owner of the registered domain name. It is a querying database that is used to record the registered users. WHOIS is...

4 minutes read.

Python Functionalities of Pillow Module

This instructional exercise is about "Pillow" library, one of the significant libraries of python for picture control. Pillow library of python, is an easy to use and also open source...

14 minutes read.

How to check the version of the Python Interpreter?

As we all know what an interpreter is, and how important it is. We should also be aware of the fact that it is important to have knowledge of the...

2 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.