Declaration and manipulation of dictionaries in Python

Dictionaries

Like a list, a dictionary is a collection of many values. But unlike indexes for lists, dictionary indexes can use many different data types, not just integers. Indexes for dictionaries are called keys, and a key with its associated value is called a key-value pair.

Declare a dictionary

A dictionary is declared as follows:
Dog = {'size': 'great', ‘color’: ‘negro’}

Access values ​​from a dictionary

This assigns a dictionary to the variable Dog. The keys to this dictionary are ‘size’ and 'color'. The values ​​for these keys are 'large', ‘negro’, respectively. You can access these values ​​through their keys:
print(Perro['tamaño']) 

'grande' 

print('Mi perro ' + Perro['color']) 

'Mi Perro es negro.'
Dictionaries can still use integer values ​​as keys, like lists use integers for indices, but they don't have to start at 0 and they can be any number.
Dicc = {12345: 'combinacion', 42: 'datos'}

The keys methods(), values(), y items()

There are three dictionary methods that will return the list values ​​of the Keys, values ​​or both keys and dictionary values: keys (), values () y items (). The values ​​returned by these methods are not true lists: cannot be modified and does not have an append method (). But these types of data (dict_keys, dict_values, y dict_items, respectively) can be traversed using loops.


datos = {'color': 'rojo', 'edad': 42}

for v in datos.values():         

   print(v)

rojo
42


Here, a for loop is repeated over each of the values ​​in the spam dictionary. A for loop can also iterate over keys or both keys and values:


for k in datos.keys():    

print(k) 

color edad

for i in datos.items():

print(i) 

('color', rojo') ('edad, 42)


Using the keys methods (), the values () and the items (), a for loop can iterate over the keys, the values ​​or key-value pairs in a dictionary, respectively. notice that the values ​​in the dict_items value returned by the items method () are Tuples of the key and the value. If you want a true list of one of these methods, pass your list return Value to the list function ().


datos = {'color': 'rojo', 'edad': 42}

datos.keys()dict_keys(['color', 'edad'])

list(datos.keys())

['color', 'age']


Using the keys methods (), the values () and the items (), a for loop can iterate over the keys, the values ​​or key-value pairs in a dictionary, respectively. notice that the values ​​in the dict_items value returned by the items method () are Tuples of the key and the value. If you want a true list of one of these methods, pass your list return Value to the list function ().
datos = {'color': 'rojo', 'edad': 42}

datos.keys()

dict_keys(['color', 'edad'])

list(datos.keys()

['color', 'age']


Check if a key or value exists in a dictionary

You can also use these operators to see if a certain key or value exists in a dictionary.
>>> spam = {'name': 'Zophie', 'age': 7}

>>> 'name' in spam.keys()True

>>> 'Zophie' in spam.values()True

>>> 'color' in spam.keys

()False

The get method()

It is tedious to check if a key exists in a dictionary before accessing the value of that key. Fortunately, dictionaries have a get method () which takes two arguments: the key of the value to retrieve and a reserve value to return if that key does not exist:


picnicItems = {'manzanas': 5, 'vasos': 2}

>>> 'yo tengo ' + str(picnicItems.get('vasos', 0)) + ' vasos.''Yo tengo 2 vasos.'

>>> 'Yo tengo' + str(picnicItems.get('eggs', 0)) + ' eggs.''Yo tengo 0 eggs.

Conclusion

In this article we review how to create and manipulate dictionaries in Python, which in a way is similar to lists, With these tools we can develop Python programs applying data structures.
Remember to check the other Articles on our blog, to learn but.

Leave a Reply

Your email address will not be published. Required fields are marked *

sixteen + 14 =