Associative lists in Python?


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 = []

asked by JetLagFox 01.01.2018 в 17:13

1 answer


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
>>> print(d["key2"]) # accedemos a 5 mediante la clave key2
answered by 01.01.2018 / 17:19