Python Dictionary Methods: Comprehensive Guide to Built-in Functions
Explore Python's built-in dictionary methods with detailed descriptions. This guide covers essential methods such as clear()
, copy()
, and fromkeys()
, providing you with a complete understanding of how to manipulate and utilize dictionaries effectively.
Python Dictionary Methods
Python provides several built-in methods to work with dictionaries. Below is a list of these methods along with their descriptions:
Method | Description |
---|---|
clear() | Removes all the elements from the dictionary |
copy() | Returns a copy of the dictionary |
fromkeys() | Returns a dictionary with the specified keys and value |
get() | Returns the value of the specified key |
items() | Returns a list containing a tuple for each key-value pair |
keys() | Returns a list containing the dictionary's keys |
pop() | Removes the element with the specified key |
popitem() | Removes the last inserted key-value pair |
setdefault() | Returns the value of the specified key. If the key does not exist: insert the key, with the specified value |
update() | Updates the dictionary with the specified key-value pairs |
values() | Returns a list of all the values in the dictionary |
Example Usage of Dictionary Methods
clear() Method
mydict = {"name": "Alice", "age": 25, "city": "New York"}
mydict.clear()
print(mydict)
Output
{}
copy() Method
mydict = {"name": "Alice", "age": 25, "city": "New York"}
newdict = mydict.copy()
print(newdict)
Output
{'name': 'Alice', 'age': 25, 'city': 'New York'}
fromkeys() Method
keys = ('name', 'age', 'city')
value = 'unknown'
newdict = dict.fromkeys(keys, value)
print(newdict)
Output
{'name': 'unknown', 'age': 'unknown', 'city': 'unknown'}
get() Method
mydict = {"name": "Alice", "age": 25, "city": "New York"}
age = mydict.get("age")
print(age)
Output
25
items() Method
mydict = {"name": "Alice", "age": 25, "city": "New York"}
items = mydict.items()
print(items)
Output
dict_items([('name', 'Alice'), ('age', 25), ('city', 'New York')])
keys() Method
mydict = {"name": "Alice", "age": 25, "city": "New York"}
keys = mydict.keys()
print(keys)
Output
dict_keys(['name', 'age', 'city'])
pop() Method
mydict = {"name": "Alice", "age": 25, "city": "New York"}
age = mydict.pop("age")
print(age)
print(mydict)
Output
25
{'name': 'Alice', 'city': 'New York'}
popitem() Method
mydict = {"name": "Alice", "age": 25, "city": "New York"}
item = mydict.popitem()
print(item)
print(mydict)
Output
('city', 'New York')
{'name': 'Alice', 'age': 25}
setdefault() Method
mydict = {"name": "Alice", "age": 25}
city = mydict.setdefault("city", "Unknown")
print(city)
print(mydict)
Output
Unknown
{'name': 'Alice', 'age': 25, 'city': 'Unknown'}
update() Method
mydict = {"name": "Alice", "age": 25}
mydict.update({"city": "New York", "email": "alice@example.com"})
print(mydict)
Output
{'name': 'Alice', 'age': 25, 'city': 'New York', 'email': 'alice@example.com'}
values() Method
mydict = {"name": "Alice", "age": 25, "city": "New York"}
values = mydict.values()
print(values)
Output
dict_values(['Alice', 25, 'New York'])