A dictionary in Python is a mutable and unordered sequence of elements. Like lists, dictionaries in Python are built-in data structures. Python dictionaries are indexed by keys, which can be any immutable type. Each item that is contained in a dictionary is a key: value pair. Keys within one dictionary must be unique.
1. Creating a Dictionary in Python
A Python dictionary is created by specifying a set of key: value pairs inside curly brackets { }.
A dictionary can also be created from sequences of key-value pairs using the dict() constructor as shown below:
2. Accessing Items of a Dictionary
Items of a dictionary can be accessed by referring to its key name inside square brackets. Using a non-existent key produces an error.
3. Modifying Values
The value of a specific item of a dictionary can be changed by referring to its key name.
4. Checking Key Existence Using the 'in' Keyword
The 'in' keyword can be used to check if a specified key exists in a dictionary.
5. Length of a Dictionary
The len() method can be used to check how many items (key-value pairs) a dictionary contains.
6. Adding an Item to a Dictionary
An item can be added to a dictionary by using a new key and assigning a value to it.
7. Removing an Item from a Dictionary
An item can be removed from a dictionary using the 'del' keyword.
It is also possible to delete the dictionary completely using the del keyword.
8. Dictionary Methods
Like other data structures, dictionaries in Python also support some built-in methods to perform common operations. Some of the most commonly used methods are given below:
dict.keys()
It returns a view object containing the keys of the dictionary as a list.
dict.values()
It returns a view object containing the values of the dictionary as a list.
dict.items()
It returns a view object containing the key-value pairs of the dictionary as tuples in a list.
dict.get()
It returns the value of the item with the specified key.
It is possible to specify an optional value as the second argument to this method. If specified, this value is returned if the specified key does not exist.
dict.pop()
It removes the item with the specified key from the dictionary. The value of the removed item is the return value of this method.
It is possible to specify an optional value as the second argument to this method. If specified, this value is returned if the specified key does not exist.
dict.popitem()
It removes the item that was last inserted into the dictionary. This method returns the removed item as a tuple.
dict.clear()
It removes all the items from a dictionary.
dict.copy()
It returns a copy of the specified dictionary.
dict.fromkeys()
It returns a dictionary with the specified keys and values. The first argument to this method is required and is an iterable specifying the keys of the new dictionary. The second argument is optional and is the value for all keys of the new dictionary. If the second argument is not specified, all keys of the new dictionary have a default value 'None'.
dict.setdefault()
It returns the value of the item with the specified key. If the specified key does not exist in the dictionary, this method inserts the key with the specified value.
dict.update()
It inserts the specified items into the dictionary. The specified items can be a dictionary or an iterable object with key-value pairs.