Tutorial Gateway

  • C
  • C#
  • Python
  • SQL
  • Java
  • JS
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Go Programs
    • Python Programs
    • Java Programs
  • MySQL

Python Dictionary pop function

Python pop Dictionary function is used to remove an item at a given key position, and prints the removed value. The syntax behind this dictionary pop function is:

dictionary_name.pop(key, default_value)

Python Dictionary pop Example

The dictionary pop function removes key-value pairs at a given key and prints the value.

# Python Dictionary pop Example

myDict = {1: 'apple', 2: 'Banana' , 3: 'Orange', 4: 'Kiwi'}
print("Dictionary Items: ", myDict)

# Pop Values
print("\nRemoved Item      :  ",  myDict.pop(3))
print("Dictionary Items  :  ",  myDict)

# Pop Values
print("\nRemoved Item      :  ",  myDict.pop(1))
print("Dictionary Items  :  ",  myDict)
Python Dictionary pop 1

Dictionary pop function Example 2

In this program, we are trying to pop or remove a non-exiting Dictionary item. As you can see, Python is throwing an error.

# Python Dictionary pop Example

myDict = {1: 'apple', 2: 'Banana' , 3: 'Orange', 4: 'Kiwi'}
print("Dictionary Items: ", myDict)

# Pop Non-existing Values
print("\nRemoved Item      :  ",  myDict.pop(5))
print("Dictionary Items  :  ",  myDict)
Python Dictionary pop 2

Pop Dictionary items Example 3

In this program, we are using the second argument to display the default value. The below dictionary pop code returns Sorry!! No Item exits message if you are trying to remove the non-existing item from the dictionary.

# Python Dictionary pop Example

myDict = {1: 'apple', 2: 'Banana' , 3: 'Orange', 4: 'Kiwi'}
print("Dictionary Items: ", myDict)

# Pop Non-existing Values
print("\nRemoved Item      :  ",  myDict.pop(5, 'Sorry!! No Item exists'))
Python Dictionary pop 3

Filed Under: Python

  • Download and Install Python
  • Python Arithmetic Operators
  • Python Assignment Operators
  • Python Bitwise Operators
  • Python Comparison Operators
  • Python Logical Operators
  • Python If Statement
  • Python If Else
  • Python Elif Statement
  • Python Nested If
  • Python For Loop
  • Python While Loop
  • Python Break
  • Python Continue
  • Python Dictionary
  • Python datetime
  • Python String
  • Python Set
  • Python Tuple
  • Python List
  • Python List Comprehensions
  • Python Lambda Function
  • Python Functions
  • Python Types of Functions
  • Python Iterator
  • Python File Handling
  • Python Directory
  • Python Class
  • Python classmethod
  • Python Inheritance
  • Python Method Overriding
  • Python Static Method
  • Connect Python and SQL Server
  • Python SQL Create DB
  • Python SQL Select Top
  • Python SQL Where Clause
  • Python SQL Order By
  • Python SQL Select Statement
  • Python len Function
  • Python max Function
  • Python map Function
  • Python print Function
  • Python sort Function
  • Python range Function
  • Python zip Function
  • Python Math Functions
  • Python String Functions
  • Python List Functions
  • Python NumPy Array
  • NumPy Aggregate Functions
  • NumPy Arithmetic Operations
  • Python Numpy Bitwise operators
  • Numpy Comparison Operators
  • Numpy Exponential Functions
  • Python Numpy logical operators
  • Python numpy String Functions
  • NumPy Trigonometric Functions
  • Python random Array
  • Python numpy concatenate
  • Python numpy Array shape
  • Python pandas DataFrame
  • Pandas DataFrame plot
  • Python Series
  • Python matplotlib Histogram
  • Python matplotlib Scatter Plot
  • Python matplotlib Pie Chart
  • Python matplotlib Bar Chart
  • Python List Length
  • Python sort List Function
  • Python String Concatenation
  • Python String Length
  • Python substring
  • Python Programming Examples

Copyright © 2021· All Rights Reserved by Suresh.
About | Contact | Privacy Policy