Associative lists in Python?

1

In Python, from what I understand, the closest thing to arrays are the listas and the tuples . I would like to know if there is any way that at least, by entering a new datum in a list, you can give it a name, as if it were an associative array. That way when I need to use that information I would look for it by the name I gave it and not by the position in the list, something that can vary.

At the moment I only use lists in Python:

info_juego = []

info_juego.append(dato)
    
asked by JetLagFox 01.01.2018 в 17:13
source

1 answer

2

If you want to access an element using a key in an iterable one then you should use a dictionary, for example:

>>> d = {"key1": 100}
>>> d["key2"] = 5  # guarda el valor 5 con la clave key2
>>> print(d["key1"])  # accedemos a 100 mediante la clave key1
100
>>> print(d["key2"]) # accedemos a 5 mediante la clave key2
5
    
answered by 01.01.2018 / 17:19
source