A dictionary is an unordered set or collection of items or objects where unique keys are mapped with values. These keys are used to access the corresponding paired value. While the keys are unique, values can be common and repeated. The data type of a value is also mutable and can be changed whereas, the data type of keys must be immutable such as strings, numbers or tuples. In Python, dictionaries are defined by adding curly brackets followed by adding the key: value pair which is separated by a colon as shown below.
How is it different from a list?
A dictionary is a composite data type in Python which resembles a list. However, a list has an ordered set of objects which can iterate and can be referenced and accessed by an index number unlike a dictionary which is unordered and has a key:value pair where values are accessed by keys.
Accessing an item in a dictionary
In order to access a value in a dictionary, you can write the variable name of the dictionary followed by squared brackets inside which you can type in the key name to access its value. If you want to access a particular value with keys that contain many values, you can use indexing as shown below:
dictionary_list = {'name': 'Ariel', 'hobbies': ['painting' , 'singing' , 'cooking'] }
print ("dictionary_list['name']:", dictionary_list['name'])
print ("dictionary_list['hobbies']:", dictionary_list['name'])
//use index to access specific value
print ("dictionary_list['hobbies'][0]:", dictionary_list['name'][0])
print ("dictionary_list['hobbies'][1]:", dictionary_list['name'][1])
print ("dictionary_list['hobbies'][2]:", dictionary_list['name'][2])
In the above example, the existing key ‘name’ is used to accessed its value ‘Ariel’. However, if a key does not exist is called, it would provide an error.
Updating a dictionary
You can also update your dictionary in order to add a new entry or key:pair value, modify existing data or even delete data.
In order to copy a key:value pair, an existing dictionary should not be assigned to a new dictionary (for instance, dictionary_1 = dictionary_2) as changes made in dictionary_1 will affect dictionary_2 as well. As a result, the copy() can be used to make copies of a dictionary to avoid such problem.
With the fromkeys() method, you can create a new dictionary from a sequence of elements which will be used as keys. These keys will be provided with a value.
This method is used to return the value of the selected key. If the key exists in the dictionary, the value is returned as it is in the dictionary. If the key does not exist, it can be added with a chosen value.
dog = { "breed": "labrador", "color": "dusty white", "sex": "female" }
x = dog.setdefault("color", "black")
print(x)
Output:
dusty white
Using update() method
The values within a dictionary can be updated or changed by assigning a new value to the specified key.
dict = {1: "one", 2: "three"}
dict_update = {2: "two"}
#value of key 2 is updated
dict.update(dict_update)
print(dict)
Output:
{1: 'one', 2: 'two'}
Using values() method
A list of all the values within the dictionary will be listed.
dog = { "breed": "labrador", "color": "dusty white", "sex": "female" }
x = dog.values()
print(x)