TheDeveloperBlog.com

Home | Contact Us

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

Python Dictionary pop() method with Examples

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

<< Back to PYTHON

Python Dictionary pop() Method

Python pop() method removes an element from the dictionary. It removes the element which is associated to the specified key.

If specified key is present in the dictionary, it remove and return its value.

If the specified key is not present, it throws an error KeyError.

Signature

pop(key[, default])

Parameters

key: A key to delete value associated to it.

defaults: If key is not present, defaults value is returned.

Return

It removes and returns value associated to the specified key.

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

Python Dictionary pop() Method Example 1

A simple example to pop an element from the dictionary. It returns popped value. See the example below.

# Python dictionary pop() Method
# Creating a dictionary
inventory = {'shirts': 25, 'paints': 220, 'shock': 525, 'tshirts': 217}
# Calling method
element = inventory.pop('shirts')
# Displaying result
print(element)

Output:

25

Python Dictionary pop() Method Example 2

If the key is not present, it returns an error KeyError. See the example below.

# Python dictionary pop() Method
# Creating a dictionary
inventory = {'shirts': 25, 'paints': 220, 'shock': 525, 'tshirts': 217}
# Calling method
element = inventory.pop('shoes')
# Displaying result
print(element)

Output:

KeyError: 'shoes'

Python Dictionary pop() Method Example 3

If key is not present, we can set default value to avoid the error KeyError. See the example.

# Python dictionary pop() Method
# Creating a dictionary
inventory = {'shirts': 25, 'paints': 220, 'shock': 525, 'tshirts': 217}
# Calling method
element = inventory.pop('shoes',100)
# Displaying result
print(element)

Output:

100

Python Dictionary pop() Method Example 4

See one more example to understand the functioning of pop() method.

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

Output:

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

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