TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python Dictionary popitem() method with Examples

Python Dictionary popitem() method with Examples on clear(), copy(), fromkeys(), get(), len(), items(), keys(), setdefault(), values(), ascii(), enumerate(), update(), pop() etc.

<< Back to PYTHON

Python Dictionary popitem() Method

Python popitem() method removes an element from the dictionary. It removes arbitrary element and return its value. If the dictionary is empty, it returns an error KeyError. Syntax of this method is given below.

Signature

popitem()

Parameters

No parameter

Return

It returns the popped element.

Let's see some examples of popitem() method to understand it's functionality.

Python Dictionary popitem() Method Example 1

Let's first see a simple example to remove an element using popitem() method.

# Python dictionary popitem() Method
# Creating a dictionary
inventory = {'shirts': 25, 'paints': 220, 'shocks': 525, 'tshirts': 217}
# Displaying result
print(inventory)
p = inventory.popitem()
print("Removed",p)
print(inventory)

Output:

{'shirts': 25, 'paints': 220, 'shocks': 525, 'tshirts': 217}
Removed ('tshirts', 217)
{'shirts': 25, 'paints': 220, 'shocks': 525}

Python Dictionary popitem() Method Example 2

If the dictionary is empty, it returns an error KeyError. See the example below.

# Python dictionary popitem() Method
# Creating a dictionary
inventory = {}
# Displaying result
print(inventory)
p = inventory.popitem()
print("Removed",p)
print(inventory)

Output:

KeyError: 'popitem(): dictionary is empty'

Python Dictionary popitem() Method Example 3

In this example, we are removing and updating the dictionary to understand the functioning of this method.

# Python dictionary popitem() Method
# Creating a dictionary
inventory = {'shirts': 25, 'paints': 220, 'shocks': 525, 'tshirts': 217}
# Displaying result
print(inventory)
p = inventory.popitem()
print("Removed",p)
print(inventory)
inventory.update({'pajama':117})
print(inventory)

Output:

{'shirts': 25, 'paints': 220, 'shocks': 525, 'tshirts': 217}
Removed ('tshirts', 217)
{'shirts': 25, 'paints': 220, 'shocks': 525}
{'shirts': 25, 'paints': 220, 'shocks': 525, 'pajama': 117}

Next TopicPython Dictionary




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf