Python dictionary functions
Here’s a list of the functions and methods associated with dictionaries in Python:
Dictionary Methods
dict.clear()
: Removes all items from the dictionary.- Example:
my_dict.clear()
- Example:
dict.copy()
: Returns a shallow copy of the dictionary.- Example:
new_dict = my_dict.copy()
- Example:
dict.fromkeys(seq[, value])
: Creates a new dictionary with keys fromseq
and values set tovalue
(default isNone
).- Example:
new_dict = dict.fromkeys(['a', 'b', 'c'], 0)
- Example:
dict.get(key[, default])
: Returns the value forkey
ifkey
is in the dictionary; otherwise, it returnsdefault
(orNone
if no default is provided).- Example:
value = my_dict.get('key', 'default_value')
- Example:
dict.items()
: Returns a view object that displays a list of dictionary's(key, value)
tuple pairs.- Example:
items = my_dict.items()
- Example:
dict.keys()
: Returns a view object that displays a list of all the keys in the dictionary.- Example:
keys = my_dict.keys()
- Example:
dict.pop(key[, default])
: Removes the specified key and returns the corresponding value. If the key is not found,default
is returned if provided; otherwise, aKeyError
is raised.- Example:
value = my_dict.pop('key', 'default_value')
- Example:
dict.popitem()
: Removes and returns the last(key, value)
pair inserted into the dictionary.- Example:
last_item = my_dict.popitem()
- Example:
dict.setdefault(key[, default])
: Returns the value ofkey
if it exists. If not, insertskey
with the valuedefault
and returnsdefault
.- Example:
value = my_dict.setdefault('key', 'default_value')
- Example:
dict.update([other])
: Updates the dictionary with elements from another dictionary or from an iterable of key-value pairs.
- Example:
my_dict.update(another_dict)
dict.values()
: Returns a view object that displays a list of all the values in the dictionary.
- Example:
values = my_dict.values()
Common Built-in Functions with Dictionaries
len(dict)
: Returns the number of key-value pairs in the dictionary.- Example:
length = len(my_dict)
- Example:
sorted(dict)
: Returns a sorted list of the dictionary’s keys.- Example:
sorted_keys = sorted(my_dict)
- Example:
Summary of Dictionary Methods and Functions
Method/Function | Description |
---|---|
dict.clear() | Removes all items from the dictionary. |
dict.copy() | Returns a shallow copy of the dictionary. |
dict.fromkeys(seq) | Creates a dictionary from a sequence of keys. |
dict.get(key) | Returns the value for a given key, or default . |
dict.items() | Returns a view of the dictionary's key-value pairs. |
dict.keys() | Returns a view of the dictionary's keys. |
dict.pop(key) | Removes a key and returns its value. |
dict.popitem() | Removes and returns the last inserted key-value pair. |
dict.setdefault() | Returns a key's value or sets it if not present. |
dict.update(other) | Updates the dictionary with another dictionary. |
dict.values() | Returns a view of the dictionary's values. |
len(dict) | Returns the number of items in the dictionary. |
sorted(dict) | Returns a sorted list of dictionary keys. |
These methods and functions provide a wide range of operations to manage, update, and retrieve data from dictionaries in Python.