Skip to content

Python dictionary methods

  • by

Python dictionary methods are built-in functions that can be called on dictionary objects to perform various operations. These methods provide a way to manipulate, access, and modify dictionaries in Python. Some common dictionary methods include:

clear(): Removes all key-value pairs from the dictionary.

my_dict.clear()

copy(): Returns a shallow copy of the dictionary.

new_dict = my_dict.copy()

get(key[, default]): Returns the value associated with the given key. If the key doesn’t exist, it returns the default value (or None if not specified).

value = my_dict.get(key)

items(): Returns a view object that contains the key-value pairs of the dictionary as tuples.

items = my_dict.items()

keys(): Returns a view object that contains the keys of the dictionary.

keys = my_dict.keys()

values(): Returns a view object that contains the values of the dictionary.

values = my_dict.values()

pop(key[, default]): Removes and returns the value associated with the given key. If the key doesn’t exist, it returns the default value (or raises a KeyError if not specified).

value = my_dict.pop(key)

popitem(): Removes and returns the last inserted key-value pair as a tuple.

item = my_dict.popitem()

update(other_dict): Updates the dictionary with the key-value pairs from another dictionary or an iterable of key-value pairs.

my_dict.update(other_dict)

setdefault(key[, default]): Returns the value associated with the given key. If the key doesn’t exist, it sets the key with the default value (or None if not specified) and returns the default value.

value = my_dict.setdefault(key[, default])

Here’s a tabular format presenting the common dictionary methods along with their descriptions:

MethodDescription
clear()Removes all key-value pairs from the dictionary.
copy()Returns a shallow copy of the dictionary.
get(key[, default])Returns the value associated with the given key.
items()Returns a view object that contains the key-value pairs of the dictionary as tuples.
keys()Returns a view object that contains the keys of the dictionary.
values()Returns a view object that contains the values of the dictionary.
pop(key[, default])Removes and returns the value associated with the given key.
popitem()Removes and returns the last inserted key-value pair as a tuple.
update(other_dict)Updates the dictionary with the key-value pairs from another dictionary or an iterable of key-value pairs.
setdefault(key[, default])Returns the value associated with the given key. If the key doesn’t exist, it sets the key with the default value and returns the default value.

Python dictionary methods example

Here’s an example that demonstrates multiple dictionary methods in a single code snippet:

my_dict = {"name": "John", "age": 25}

# Using dictionary methods
my_dict.clear()
print(my_dict) 

my_dict = {"name": "John", "age": 25}
new_dict = my_dict.copy()
print(new_dict)  

value = my_dict.get("name")
print(value)  

items = my_dict.items()
print(items)  

keys = my_dict.keys()
print(keys)  

values = my_dict.values()
print(values) 

age = my_dict.pop("age")
print(age)  
print(my_dict)  

item = my_dict.popitem()
print(item)  
print(my_dict) 

other_dict = {"city": "New York"}
my_dict.update(other_dict)
print(my_dict)  

age = my_dict.setdefault("age", 30)
city = my_dict.setdefault("city", "Unknown")
print(age)  
print(city)  
print(my_dict) 

Output:

Python dictionary methods

Do comment if you have any doubts or suggestions on this Python basic topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading