Dictionaries in Python are a structure that allows you to store the elements of a data structure that are given a name or key (key). In this way, instead of accessing the dictionary element through the index it occupies, it is accessed by the name it has. We can define a dictionary as a set of key-value pairs, where the key is the name given to a value. The values in a dictionary can be of any form: numbers, stringslists…
Python Dictionaries
Let’s look at an example of dictionaries in Python:
#Dictionaries in Python {‘A’: 0, ‘B’: 1, ‘C’: 2, ‘D’: 3, ‘E’: 4, ‘F’: 5, ‘G’: 6, ‘H’ : 7, ‘I’: 8, ‘J’: 9, ‘K’: 10, ‘L’: 11, ‘M’: 12, ‘N’: 13, ‘O’: 14, ‘P’: 15 , ‘Q’: 16, ‘R’: 17, ‘S’: 18, ‘T’: 19, ‘U’: 20, ‘V’: 21, ‘W’: 22, ‘X’: 23, ‘ Y’: 24, ‘Z’: 25}
{‘A’: 0,
‘B’: 1,
‘C’: 2,
‘D’: 3,
‘E’: 4,
‘F’: 5,
‘G’: 6,
‘H’: 7,
‘I’: 8,
‘J’: 9,
‘K’: 10,
‘L’: 11,
‘M’: 12,
‘N’: 13,
‘O’: 14,
‘P’: 15,
‘Q’: 16,
‘R’: 17,
‘S’: 18,
‘T’: 19,
‘U’: 20,
‘V’: 21,
‘W’: 22,
‘X’: 23,
‘Y’: 24,
‘Z’: 25}
In this case, we assign a number to each letter, in this way we can access each of the values and modify the value of these.
In dictionaries, keys They are unique. We cannot have several keys with different values.
So, dictionaries in Python are defined as follows: if you look closely, the way to write them is the same as in sets, since they are enclosed in curly braces.
If we assign a value to multiple indices, only one value will be associated:
#Python Dictionaries # {key1: value1, key2: value2, …} {0: ‘house’, 0: ‘garden’, 0: ‘tree’}
{0: ‘tree’}
The simplest way to extract an element from a dictionary is with the my_dict command:
#Dictionaries in Python my_dict = {«Maite»: 20, «Javier»: 45} my_dict
{‘Maite’: 20, ‘Javier’: 45}
With dictionaries in Python we cannot use the same logic as in a tuple to get values from a certain position, because it throws an error.
This happens because It is a structure of key valuethat is, we map a value to a keywhich means that we must obtain the key in order to obtain the value that we want to get from the dictionary.
my_dict [1]
This means that If we want to extract an element, we use its key. To extract the element we must do it by the method keysfor his key or by its value:
my_dict [‘Maite’]
twenty
Thus, it tells us that the value associated with ‘Maite’ is «20».
There are many ways to navigate a dictionary, let’s look at some with an example:
for k, v in my_dict.items(): print(k, v)
May 20
Javier 45
#Dictionaries in Python keys = my_dict.keys () values = my_dict.values () print (keys) print (values)
dict_values ([‘Maite’, ‘Javier’])
dict_values ([’20’, ’45’])
print (type (keys)) print (type (values))
print (list (keys)) type (list (keys))
[‘Maite’, ‘Javier’]
list
Let’s see one more exercise, in which we want to extract the value when key = ‘Bilbo’ in the following list of dictionaries [{‘Bilbo’, ‘Ian’, ‘Frodo’, ‘Elijah’}, {‘Bilbo’, ‘Martín’, ‘Thorin’, ‘Richard’}]
#Dictionaries in Python dlist = [{‘Bilbo’, ‘Ian’, ‘Frodo’, ‘Elijah’}, {‘Bilbo’, ‘Martín’, ‘Thorin’, ‘Richard’}]
print (type (dlist))
for d in dlist: print (d [«Bilbo»])
Ian
Martin
Whats Next?
Big Data is one of the areas in which the most jobs are offered. If you want to access this type of highly demanded and well-paid job options, we have for you the Big Data, Artificial Intelligence & Machine Learning Full Stack Bootcamp, an intensive training in which you will acquire all the theoretical and practical knowledge that will allow you to get the job. of your dreams. Don’t wait any longer to follow your path to success and enter to request information now!