Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Pop() Method | Dictionary
Python Data Structures
course content

Course Content

Python Data Structures

Python Data Structures

1. List
2. Dictionary
3. Tuple
4. Set

bookPop() Method

Dictionaries also have a pop() method that removes a key-value pair by its key and returns its value. If the specified key isn't found in the dictionary, a KeyError exception is raised.

The syntax is:

12345
fruits = { 'apple': 2, 'orange': 3, 'grapes': 4 } first_element = fruits.pop('apple') print(fruits) print(first_element)
copy

In the following example, a KeyError exception will be raised since the dictionary does not contain the key 'guava'.

12
dict_1 = { 'apple': 2, 'orange': 3, 'grapes': 4 } element = dict_1.pop('guava')
copy

Task

Consider this dictionary:

You need to remove the entries with the keys 1, 5, and 6 using the pop() method.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 6
toggle bottom row

bookPop() Method

Dictionaries also have a pop() method that removes a key-value pair by its key and returns its value. If the specified key isn't found in the dictionary, a KeyError exception is raised.

The syntax is:

12345
fruits = { 'apple': 2, 'orange': 3, 'grapes': 4 } first_element = fruits.pop('apple') print(fruits) print(first_element)
copy

In the following example, a KeyError exception will be raised since the dictionary does not contain the key 'guava'.

12
dict_1 = { 'apple': 2, 'orange': 3, 'grapes': 4 } element = dict_1.pop('guava')
copy

Task

Consider this dictionary:

You need to remove the entries with the keys 1, 5, and 6 using the pop() method.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 6
toggle bottom row

bookPop() Method

Dictionaries also have a pop() method that removes a key-value pair by its key and returns its value. If the specified key isn't found in the dictionary, a KeyError exception is raised.

The syntax is:

12345
fruits = { 'apple': 2, 'orange': 3, 'grapes': 4 } first_element = fruits.pop('apple') print(fruits) print(first_element)
copy

In the following example, a KeyError exception will be raised since the dictionary does not contain the key 'guava'.

12
dict_1 = { 'apple': 2, 'orange': 3, 'grapes': 4 } element = dict_1.pop('guava')
copy

Task

Consider this dictionary:

You need to remove the entries with the keys 1, 5, and 6 using the pop() method.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Dictionaries also have a pop() method that removes a key-value pair by its key and returns its value. If the specified key isn't found in the dictionary, a KeyError exception is raised.

The syntax is:

12345
fruits = { 'apple': 2, 'orange': 3, 'grapes': 4 } first_element = fruits.pop('apple') print(fruits) print(first_element)
copy

In the following example, a KeyError exception will be raised since the dictionary does not contain the key 'guava'.

12
dict_1 = { 'apple': 2, 'orange': 3, 'grapes': 4 } element = dict_1.pop('guava')
copy

Task

Consider this dictionary:

You need to remove the entries with the keys 1, 5, and 6 using the pop() method.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Section 2. Chapter 6
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
some-alt