Python Dictionary Methods
Python dictionaries are unordered collections of key-value pairs, where each key must be unique. They are implemented as hash tables, providing fast access to values based on their keys. Python provides several built-in dictionary methods to perform common operations on dictionaries. Here are some of the commonly used methods with examples:dict.get(key, default)
: Returns the value associated with the given key, if it exists in the dictionary, otherwise returns the specified default value.
Example:
pythonmy_dict = {'name': 'Santi', 'age': 40, 'gender': 'female'}
# Accessing values using get() method
name = my_dict.get('name', 'Unknown')
age = my_dict.get('age', None)
city = my_dict.get('city', 'Unknown')
print(name) # Output: 'Santi'
print(age) # Output: 40
print(city) # Output: 'Unknown'
dict.keys()
: Returns a view object that displays a list of all the keys in the dictionary.
Example:
pythonmy_dict = {'name': 'Santi', 'age': 40, 'gender': 'female'}
# Accessing keys using keys() method
keys = my_dict.keys()
print(keys) # Output: dict_keys(['name', 'age', 'gender'])
print(list(keys)) # Output: ['name', 'age', 'gender']
dict.values()
: Returns a view object that displays a list of all the values in the dictionary.
Example:
pythonmy_dict = {'name': 'Santi', 'age': 40, 'gender': 'female'}
# Accessing values using values() method
values = my_dict.values()
print(values) # Output: dict_values(['Santi', 40, 'female'])
print(list(values)) # Output: ['Santi', 40, 'female']
dict.items()
: Returns a view object that displays a list of all the key-value pairs in the dictionary as tuple pairs.
Example:
pythonmy_dict = {'name': 'Santi', 'age': 40, 'gender': 'female'}
# Accessing key-value pairs using items() method
items = my_dict.items()
print(items) # Output: dict_items([('name', 'Santi'), ('age', 40), ('gender', 'female')])
print(list(items)) # Output: [('name', 'Santi'), ('age', 40), ('gender', 'female')]
dict.update(other_dict)
: Updates the dictionary with key-value pairs from another dictionary.
Example:
pythonmy_dict = {'name': 'Santi', 'age': 40}
# Updating dictionary using update() method
my_dict.update({'age': 41, 'city': 'Kolkata'})
print(my_dict) # Output: {'name': 'Santi', 'age': 41, 'city': 'Kolkata'}
dict.pop(key, default)
: Removes and returns the value associated with the given key from the dictionary. If the key does not exist, it returns the specified default value.
Example:
pythonmy_dict = {'name': 'Santi', 'age': 40, 'gender': 'female'}
# Removing a key-value pair using pop() method
age = my_dict.pop('age', None)
city = my_dict.pop('city', 'Unknown')
print(age) # Output: 40
print(city) # Output: 'Unknown'
print(my_dict) # Output: {'name': 'Santi', 'gender': 'female'}
dict.clear()
: Removes all the key-value pairs from the dictionary and display empty dictionary.
Example:
pythonmy_dict = {'name': 'Santi', 'age': 40, 'gender': 'female'}
# Clearing dictionary using clear() method
my_dict.clear()
print(my_dict) # Output: {}
dict.copy()
: Creates a shallow copy of the dictionary, i.e., a new dictionary with the same key-value pairs but a different memory address.
Example:
pythonmy_dict = {'name': 'Santi', 'age': 40, 'gender': 'female'}
# Creating a shallow copy of the dictionary using copy() method
my_dict_copy = my_dict.copy()
print(my_dict_copy)
# Output: {'name': 'Santi', 'age': 40, 'gender': 'female'}
dict.setdefault(key, default)
: Returns the value associated with the given key, if it exists in the dictionary. Otherwise, sets the key with the specified default value and returns the default value.
Example:
pythonmy_dict = {'name': 'Santi', 'age': 40}
# Setting a default value for a key using setdefault() method
gender = my_dict.setdefault('gender', 'Unknown')
city = my_dict.setdefault('city', 'Kolkata')
print(gender) # Output: 'Unknown'
print(city) # Output: 'Kolkata'
print(my_dict)
# Output: {'name': 'Santi', 'age': 40, 'gender': 'Unknown', 'city': 'Kolkata'}
dict.fromkeys(iterable, value)
: Creates a new dictionary with keys from the specified iterable and values set to the given value.
Example:
pythonkeys = ['name', 'age', 'gender']
my_dict = dict.fromkeys(keys, 'Unknown')
print(my_dict)
# Output: {'name': 'Unknown', 'age': 'Unknown', 'gender': 'Unknown'}
These are some of the commonly used dictionary methods in Python. There are many other methods available in the Python documentation.