DEV Community

Usool
Usool

Posted on

A Quick Guide to Python Dictionary Methods with Examples

Introduction

Python dictionaries are powerful data structures for storing key-value pairs. Below is a quick reference to the most commonly used dictionary methods with brief examples.

1. clear()

Removes all items from the dictionary.

d = {'a': 1, 'b': 2}
d.clear()  # {}
Enter fullscreen mode Exit fullscreen mode

2. copy()

Returns a shallow copy of the dictionary.

d = {'a': 1, 'b': 2}
new_d = d.copy()  # {'a': 1, 'b': 2}
Enter fullscreen mode Exit fullscreen mode

3. fromkeys(iterable, value)

Creates a new dictionary from keys and an optional value.

keys = ['a', 'b', 'c']
d = dict.fromkeys(keys, 0)  # {'a': 0, 'b': 0, 'c': 0}
Enter fullscreen mode Exit fullscreen mode

4. get(key, default)

Returns the value of a key if it exists, otherwise returns the default value.

d = {'a': 1, 'b': 2}
d.get('a')  # 1
d.get('c', 0)  # 0
Enter fullscreen mode Exit fullscreen mode

5. items()

Returns a view object that displays a list of a dictionary's key-value pairs.

d = {'a': 1, 'b': 2}
d.items()  # dict_items([('a', 1), ('b', 2)])
Enter fullscreen mode Exit fullscreen mode

6. keys()

Returns a view object that displays a list of all the keys in the dictionary.

d = {'a': 1, 'b': 2}
d.keys()  # dict_keys(['a', 'b'])
Enter fullscreen mode Exit fullscreen mode

7. pop(key, default)

Removes and returns the value for the specified key. If the key is not found, the default value is returned.

d = {'a': 1, 'b': 2}
d.pop('a')  # 1, d = {'b': 2}
d.pop('c', 0)  # 0
Enter fullscreen mode Exit fullscreen mode

8. popitem()

Removes and returns the last inserted key-value pair as a tuple.

d = {'a': 1, 'b': 2}
d.popitem()  # ('b', 2)
Enter fullscreen mode Exit fullscreen mode

9. setdefault(key, default)

Returns the value of a key if it exists, otherwise inserts the key with a default value.

d = {'a': 1}
d.setdefault('b', 2)  # 2, d = {'a': 1, 'b': 2}
Enter fullscreen mode Exit fullscreen mode

10. update([other])

Updates the dictionary with elements from another dictionary or iterable of key-value pairs.

d = {'a': 1, 'b': 2}
d.update({'b': 3, 'c': 4})  # {'a': 1, 'b': 3, 'c': 4}
Enter fullscreen mode Exit fullscreen mode

11. values()

Returns a view object that displays a list of all the values in the dictionary.

d = {'a': 1, 'b': 2}
d.values()  # dict_values([1, 2])
Enter fullscreen mode Exit fullscreen mode

12. len(dict)

Returns the number of key-value pairs in a dictionary.

d = {'a': 1, 'b': 2}
len(d)  # 2
Enter fullscreen mode Exit fullscreen mode

13. in

Checks if a key exists in a dictionary.

d = {'a': 1, 'b': 2}
'a' in d  # True
'c' in d  # False
Enter fullscreen mode Exit fullscreen mode

14. del

Deletes a key-value pair from the dictionary.

d = {'a': 1, 'b': 2}
del d['a']  # d = {'b': 2}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Python dictionaries provide a wide range of methods for key-value management. Whether retrieving, updating, or deleting data, these methods offer efficient ways to manipulate dictionaries in Python.

Top comments (0)