Python dictionary functions


Here’s a list of the functions and methods associated with dictionaries in Python:

Dictionary Methods

  1. dict.clear(): Removes all items from the dictionary.

    • Example: my_dict.clear()
  2. dict.copy(): Returns a shallow copy of the dictionary.

    • Example: new_dict = my_dict.copy()
  3. dict.fromkeys(seq[, value]): Creates a new dictionary with keys from seq and values set to value (default is None).

    • Example: new_dict = dict.fromkeys(['a', 'b', 'c'], 0)
  4. dict.get(key[, default]): Returns the value for key if key is in the dictionary; otherwise, it returns default (or None if no default is provided).

    • Example: value = my_dict.get('key', 'default_value')
  5. dict.items(): Returns a view object that displays a list of dictionary's (key, value) tuple pairs.

    • Example: items = my_dict.items()
  6. dict.keys(): Returns a view object that displays a list of all the keys in the dictionary.

    • Example: keys = my_dict.keys()
  7. 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, a KeyError is raised.

    • Example: value = my_dict.pop('key', 'default_value')
  8. dict.popitem(): Removes and returns the last (key, value) pair inserted into the dictionary.

    • Example: last_item = my_dict.popitem()
  9. dict.setdefault(key[, default]): Returns the value of key if it exists. If not, inserts key with the value default and returns default.

    • Example: value = my_dict.setdefault('key', 'default_value')
  10. 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)
  1. 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

  1. len(dict): Returns the number of key-value pairs in the dictionary.

    • Example: length = len(my_dict)
  2. sorted(dict): Returns a sorted list of the dictionary’s keys.

    • Example: sorted_keys = sorted(my_dict)

Summary of Dictionary Methods and Functions

Method/FunctionDescription
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.