Data types and variables
Operators
Modules and Packages
Conversion Programs
More Code Examples
Cheat Sheet

Dictionary (dict) methods in Python

Dictionaries in Python are versatile data structures that store key-value pairs. They provide efficient lookup, insertion, and deletion operations. Python dictionaries have a variety of built-in methods that facilitate manipulation and management of the data they hold.

Method dict.clear()

  • Description: Removes all items from the dictionary.
Usage:
d = {'a': 1, 'b': 2}
d.clear()
# d is now {}

Method dict.copy()

  • Description: Returns a shallow copy of the dictionary.
Usage:
d = {'a': 1, 'b': 2}
d_copy = d.copy()

Method dict.fromkeys(iterable, value=None)

  • Description: Creates a new dictionary with keys from the iterable and values set to value.
Usage:
keys = ['a', 'b', 'c']
d = dict.fromkeys(keys, 0)
# d is {'a': 0, 'b': 0, 'c': 0}

Method dict.get(key, default=None)

  • Description: Returns the value for key if key is in the dictionary, else default.
Usage:
d = {'a': 1, 'b': 2}
value = d.get('a', 0)  # Returns 1
value = d.get('c', 0)  # Returns 0

Method dict.items()

  • Description: Returns a view object that displays a list of dictionary’s key-value tuple pairs.
Usage:
d = {'a': 1, 'b': 2}
items = d.items()
# items is dict_items([('a', 1), ('b', 2)])

Method dict.keys()

  • Description: Returns a view object that displays a list of all the keys in the dictionary.
Usage:
d = {'a': 1, 'b': 2}
keys = d.keys()
# keys is dict_keys(['a', 'b'])

Method dict.pop(key, default=None)

  • Description: Removes the specified key and returns the corresponding value. If the key is not found, default is returned if provided, otherwise KeyError is raised.
Usage:
d = {'a': 1, 'b': 2}
value = d.pop('a')  # Returns 1, d is now {'b': 2}
value = d.pop('c', 0)  # Returns 0, d is still {'b': 2}

Method dict.popitem()

  • Description: Removes and returns an arbitrary (key, value) pair from the dictionary. Raises KeyError if the dictionary is empty.
Usage:
d = {'a': 1, 'b': 2}
item = d.popitem()  # Could return ('a', 1) or ('b', 2)

Method dict.setdefault(key, default=None)

  • Description: Returns the value of key if it is in the dictionary; if not, inserts key with a value of default and returns default.
Usage:
d = {'a': 1}
value = d.setdefault('a', 0)  # Returns 1
value = d.setdefault('b', 2)  # Returns 2, d is now {'a': 1, 'b': 2}

Method dict.update([other])

  • Description: Updates the dictionary with the key-value pairs from other, overwriting existing keys. other can be another dictionary or an iterable of key-value pairs.
Usage:
d = {'a': 1}
d.update({'b': 2})
# d is now {'a': 1, 'b': 2}

Method dict.values()

  • Description: Returns a view object that displays a list of all the values in the dictionary.
Usage:
d = {'a': 1, 'b': 2}
values = d.values()
# values is dict_values([1, 2])

Summary

Dictionaries in Python are powerful and efficient for managing key-value pairs. The methods provided by the dict class allow for a wide range of operations, from basic data manipulation to more complex tasks like merging dictionaries or handling missing keys gracefully. By leveraging these methods, you can perform dictionary operations in a clean and efficient manner.