×

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

Selection Sort Using Python

Selection sort is a type of sorting algorithm which is based on sorting elements in increasing order or ascending order through comparison. This sorting technique does not take extra space...

3 minutes read.

Python zip() Function

Python zip() Function The zip() function makes an iterator that aggregates elements from each of the iterables and returns an iterator of tuples. Syntax zip(*iterables) Parameter iterables: Iterator objects that will be joined together Return This function...

1 minute read.

Python Parser

Introduction : Parsing is referred to as the processing and translation of a Python program into machine language. In general, we could indeed say that the command parse is used to...

4 minutes read.

Python program to convert Celsius into Fahrenheit

Python program to convert Celsius into Fahrenheit This program explains how we can take the temperature in Celsius and convert them into Fahrenheit. Celsius Celsius, also known as centigrade, is a measurement unit...

1 minute 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 Program to Find the gcd of Two Numbers

Introduction Greatest Common Divisor is the full form of gcd. The greatest common divisor, or GCD, of two numbers, is a value that can exactly divide the two digits and is...

5 minutes read.

Explain sklearn clustering in Python

Make a connection and patterns across datasets by using clustering, one of the unsupervised machine learning approaches. Grouping is crucial because it ensures unlabelled data's natural clustering. The sample from...

7 minutes read.

Nested Tuple in Python

If you are a Python learner, this page contains all the information that helps you know about nested tuple and how to access it in python. What is a Tuple? Multiple items...

4 minutes read.

Python hash() function

Python hash() function The hash() function in Python returns the hash value of the object (if it has one).  Syntax hash(object) Parameter obj : This parameter represents the object which we need to convert into hash. Return This...

1 minute read.

Python Program to check whether a given number is Armstrong or not

Program to check whether a given number is Armstrong or not A number is said to be an Armstrong if the sum of each digit's cube of a given number equals...

1 minute read.

Python Set discard() method

Python Set discard() method The set.discard() method in Python removes a specified element from the set (if present). Syntax set.discard(value) Parameter value- This parameter represents the element to be removed from the set. Return None Example 1 # Python program explaining # the...

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

Python random.seed() function

The random module in Python produces a random number or pseudo-random data, that is, deterministic. The seed function records the state of a random function to provide the same random...

6 minutes read.

Python Syntax

Python is a strong object-oriented programming language that is simple to learn. Python was created to be a very readable programming language. The syntax of the Python programming language is...

8 minutes read.

Python Unit Test Cheat String

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.

Python Random shuffle( ) method

The shuffle() is used to change the positions of the elements in the mutable sequences. The shuffle( ) function will change the positions of the elements in the sequence of...

3 minutes read.

Important Difference between Python 2.x and Python 3.x with Example

The comparison between Python 2 and Python 3 is given in the article that follows. Python is a computer language that can perform more tasks than other languages and is...

5 minutes read.

Python Matrix Multiplication

One of the most fundamental mathematical structures, matrices are used often in many disciplines, including mathematics, physics, engineering, computer science, etc. For example, matrices and associated operations (multiplication and addition) are...

8 minutes read.

Find whether the given stringnumber is palindrome or not

Problem statement Sam is found of playing with strings. One day he thought of finding whether a string is a palindrome or not. He wanted to develop a computer process to...

2 minutes read.

Python Marshmallow

Python:  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 is said...

3 minutes read.