×

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

Convert int to Float in Python using Pandas

Introduction We have already learnt about how to convert float variables to int. Now let us know how to convert int variables to float So let us create another Dataset on which...

2 minutes read.

How to comment multiple lines in python?

How to comment multiple lines in python There are two qualities that come up with every good and successful programmer- i) Indenting the code ii) Using comments in the code Let us see how...

4 minutes read.

Python lambda() Function

In this tutorial, we will learn about a new concept known as the Lambda function. It is a relatively new concept while learning Python. Python lambda functions are anonymous functions. It...

3 minutes read.

How to Make an App with Python

In this age of mobiles, rapid mobile application development has got a lot of traction. Moreover, app developers are high in demand because of the increase in digitization. Generally, Python is...

4 minutes read.

Python program to find the greatest among two numbers

Python program to find the greatest among two numbers We have many approaches to get the largest number among the two numbers, and here we will discuss a few of them....

2 minutes read.

Python Built-in Functions

Python Built-in Functions The Python interpreter has a number of functions and types built into it that are always available. The Python built- in functions are as follow: Methods Explaining abs() The abs() function returns...

6 minutes read.

Paramiko Python Example

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.

Excel to CSV in Python

Define MS Excel Microsoft Excel is an application provided by the Microsoft corporation which allows us to build graphs to build tables, and it is also used for the macro programming...

4 minutes read.

How to create a dictionary in Python?

How to create a dictionary in python Dictionary is a data structure in Python that represents our data in the form of keys and values. Each value in a dictionary can be...

5 minutes read.

Python MySQL Database Connection

In this article, you will be able to understand how to connect to a MySQL database through Python. We generally can use many methods or modules to connect to the database...

6 minutes 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 Maurer Rose and Rhodonea Curves

In this tutorial, in Python we will make a Maurer Rose example and Rhodonea Curve example. Before we continue to see what precisely is maurer rose or a rhodonea bend...

10 minutes read.

PyPi TensorFlow

It is a free open-source library for high-performative numerical computations. The architecture of TensorFlow allows us for easy deployment and computing across a varied number of platforms and from desktops...

3 minutes read.

Introducing modern python computing in simple packages

Python is the language for newly evolving technologies and has become one of the most popular computer languages in the world. Python is used in everything right from a simple...

3 minutes read.

Excel Automation with Python

Data analysis is the upcoming technology in the IT sector; this data analysis can be performed easily with the help of data frames or by using excel sheets. The data...

4 minutes read.

Python Program for Linear Search

Introduction We employ specific algorithms to efficiently carry out our responsibilities, such as searching for an element in a given data structure. These algorithms fall under the category of searching algorithms....

4 minutes read.

Not supported between instances of str and int in python

In this article, you are going to learn why the type error occurs between instances of string and integer datatypes. Also, you will know what kind of error is the...

6 minutes read.

Python Variable Scope with Local & Non-local Examples

This article will examine Python's global, local and non-local variables and show you how to use them to write code without problems. Let's quickly review what a variable in Python is...

8 minutes read.

Python dir() function

Python dir() function The dir() function in Python returns all properties and methods of the specified object, without the values. Syntax dir([object]) Parameter object: This parameter represents the object one wants to see the valid attributes. Return This function...

2 minutes read.

Python K-Means Clustering

K-Means Clustering is a basic yet incredible calculation in information scienceThere are a plenty of true utilizations of K-Means clustering (a couple of which we will cover here)This far reaching...

25 minutes read.