Delete element in dictionary python

How to Remove an Element From Dictionary in Python

Method 1: Using the “del” statement

To remove an element from a dictionary using the del statement, “specify the key of the element you want to remove after the del keyword”. The element with the specified key will be removed from the dictionary. If the key is not found in the dictionary, a KeyError will be raised.

Example

main_dict = # Remove the element with the key 'B' del main_dict['B'] print(main_dict)

Method 2: Using the “dict.pop()” method

The “dict.pop()” method allows you to remove an element with a specified key and return its value. You must pass the key of the element you want to remove as an argument to the pop() method.

If the key is not found in the dictionary, a KeyError will be raised. However, you can also provide a default value as a second argument to the pop() method, which will be returned instead of raising a KeyError when the key is not found in the dictionary.

Example

main_dict = # Remove the element with the key 'B' main_dict.pop('B') print(main_dict)

The “dict.pop()” method removes the element with the specified key and returns its value. If the key is not found, a KeyError will be raised.

main_dict = # Remove the element with the key 'B' main_dict.pop('D') print(main_dict) 
KeyError: 'D'

Method 3: Using the popitem() method

The popitem() method “removes an element from the dictionary and returns its key and value as a tuple, (key, value).”

Example

cars = cars.popitem() print(cars)

Method 4: Using the clear() method

The clear() method removes all elements from a dictionary, making it empty.

Example

cars = cars.clear() print(cars)

Источник

Python Removing Items from a Dictionary

There are several methods to remove items from a dictionary:

Example

The pop() method removes the item with the specified key name:

Example

The popitem() method removes the last inserted item (in versions before 3.7, a random item is removed instead):

Example

The del keyword removes the item with the specified key name:

Example

The del keyword can also delete the dictionary completely:

thisdict = <
«brand»: «Ford»,
«model»: «Mustang»,
«year»: 1964
>
del thisdict
print(thisdict) #this will cause an error because «thisdict» no longer exists.

Example

The clear() keyword empties the dictionary:

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Python — Remove Dictionary Items

There are several methods to remove items from a dictionary:

Example

The pop() method removes the item with the specified key name:

Example

The popitem() method removes the last inserted item (in versions before 3.7, a random item is removed instead):

Example

The del keyword removes the item with the specified key name:

Example

The del keyword can also delete the dictionary completely:

thisdict = <
«brand»: «Ford»,
«model»: «Mustang»,
«year»: 1964
>
del thisdict
print(thisdict) #this will cause an error because «thisdict» no longer exists.

Example

The clear() method empties the dictionary:

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Читайте также:  PHP Program to show current page URL
Оцените статью