Python
Python
Python Dictionary Methods
Python provides several built-in dictionary methods that allow you to efficiently manipulate, access, and transform dictionary data. Here is a list of some important dictionary methods in Python:
Method | Description |
---|---|
dict.clear() |
Removes all items from the dictionary |
dict.copy() |
Returns a shallow copy of dictionary dict |
dict.fromkeys() |
Creates a new dictionary with keys from the given sequence |
dict.get(key, default=None) |
For key key , returns value or default if key not in dictionary |
dict.items() |
Returns a new view containing a tuple for each key value pair |
dict.keys() |
Returns a view object that displays a list of all the keys in the dictionary |
dict.values() |
Returns a view object containing all dictionary values, which can be accessed and iterated through efficiently |
dict.setdefault(key, default) |
Returns the value of a key if the key is in the dictionary else inserts the key with a value to the dictionary |
dict.update(other) |
Updates the dictionary with the elements from another dictionary or an iterable of key-value pairs. With this method, you can include new data or merge it with existing dictionary entries |
dict.pop() |
Returns and removes the element with the given key |
dict.popitem() |
Returns and removes the item that was last inserted into the dictionary |
dict.has_key(key) |
has_key() method was removed in Python 3. Use the in operator instead |
Built-in Functions with Dictionaries
Following are the built-in functions we can use with Dictionaries.
Function | Description |
---|---|
dict() |
Creates a new dictionary |
len(dict) |
Gives the total length of the dictionary. This would be equal to the number of items in the dictionary |
str(dict) |
Produces a printable string representation of a dictionary |
type(variable) |
Returns the type of the passed variable. If passed variable is dictionary, then it would return a dictionary type |
isinstance(dict) |
Checks if an object is a dictionary. Remember that custom dictionary subclasses will also return True. |
list(dict) |
Converts dictionary keys to a list |
sorted(dict) |
Returns a new sorted list of dictionary keys. Remember that sorted() returns a list, not a dictionary. If you need a sorted dictionary, you'll need to create a new one from the sorted items. |
tuple(dict) |
Converts dictionary keys to a tuple |
tuple(dict.values()) |
Converts dictionary values to a tuple |
iter(dict) |
Return an iterator over the keys of the dictionary. This is a shortcut for iter(dict.keys()) |
iter(dict.values()) |
Return an iterator over all values in a dictionary |
reversed(dict) |
Return a reverse iterator over the keys of the dictionary. This is a shortcut for reversed(dict.keys()) |
sum(dict) |
Sums dictionary keys (if they're numbers) |
max(dict) |
Find maximum keys |
min(dict) |
Find minimum keys |
sum(dict.values()) |
Sums dictionary values (if they're numbers) |
max(dict.values()) |
Find maximum values |
min(dict.values()) |
Find minimum values |
FAQs on Python Dictionary Methods
What’s the difference between clear()
and reassigning {}
?
my_dict.clear()
removes all items from the existing object.
Reassigning my_dict = {}
creates a new empty dictionary, which may not update other references to the original dictionary.
How can I check if a key exists in a dictionary?
You can use the in
operator
my_dict = {'name': 'John'}
if 'name' in my_dict:
print("Key exists!")
How do I remove a specific key-value pair from a dictionary?
You can use either the pop()
method to remove and return the value, or del
statement:
my_dict = {'a': 1, 'b': 2}
value = my_dict.pop('a') # Removes 'a' and returns 1
del my_dict['b'] # Removes 'b'