Monday, July 25, 2022

What is a Python dictionary and how to create it?

The dictionary is similar to a list. However, in the list, the index positions must be integers while in the dictionary the indexing can be achieved with any type of value. Dictionary can be taught as a mapping between a set of indices (keys) and a set of values. The key maps to a value and this pair is called a key-value pair or item. To create the empty dictionary is to use the built-in function dict which will create a new dictionary with no items. In the general form, the empty dictionary can be created using the following code.
variable_name = dict()
The previous code will create an empty dictionary.
{}
These curly brackets are an empty dictionary. In general, form to add an item to a dictionary type the following.
variable_name['key'] = 'value'
this will create an item that maps from the key 'key' to the value 'value'. Using the print function we can show the dictionary.
print("variable_name = {}".format(variable_name))
The output of the print function is given below.
variable_name = {'key': 'value'}

Example 1 Create dictionary with 4 key-value pairs.
Solution:
Cars = {'Pontiac': 'GTO', 'Chevrolet': 'Chevelle SS 454', 'Plymouth': 'Road Runner Superbird', 'Chevrolet': 'Camaro Z/28'}
print("Cars = {}".format(Cars))
The output is given below.
Cars = {'Pontiac': 'GTO', 'Chevrolet': 'Camaro Z/28', 'Plymouth': 'Road Runner Superbird'}
As you can see in the output the order of the key-value pairs is not the same. The order of items in a dictionary is unpredictable. However, this is not important since the elements of a dictionary are not indexed with integer indices. The values of the dictionary are accessed using keys. Type in the following code.
print("Cars['Plymouth'] = {}".format(Cars['Plymouth']))
The output is given below.
Road Runner Superbird
The key 'Plymouth' will always map to the value 'Road Runner Superbird' so the order of items in the dictionary does not matter. If the key does not exist in the dictionary the Python will return the KeyError exception. So we will call key 'Ford' and see what happens.
print(Cars['Ford'])
The output is given below.
KeyError: 'Ford'
So on dictionaries, we cannot use brackets to access the dictionary elements with indexes. Instead, we are using key-value notation. However, the len function works on dictionaries and as a result, it will return the number of key-value pairs.
print("len(Cars) = {}".format(len(Cars))
len(Cars) = 3
The in operator works on dictionaries and can be used to give information if something appears as a key in the dictionary.
print('Pontiac' in Cars)
The output.
True
If we use the in operator on the values it will return a False result.
print('GTO' in Cars)
The output.
False
However there is a way to implement in operator on values. First, the values are extracted using the values function.
Values = Cars.values()
print("Values = {}".format(Values))
The output.
Values = dict_values(['GTO', 'Camaro Z/28', 'Road Runner Superbird'])
The function values() extract the values from the dictionaries into the list. To check if the value 'GTO' exits using in operator type in the following code.
print('GTO' in Cars)
The output.
True

No comments:

Post a Comment