How do Python dictionaries work?

If I run the following code:

a={}
a[input("key: ")] = input("value: ")

First, the interpreter will tell me value input, and then key input.

What is the reason for this?

+4
source share
2 answers

Typically, the order of an internal expression is never guaranteed. What happens in your case is that the interpreter first finds out what needs to be placed in the dictionary, and then finds out where it should be placed. From the point of view of the translator, this is a more optimal order.

- ( "" ), , . , , .

, , :

key = input('key')
a[key] =  input('value')
+6

docs:

Python . , , .

+5

Source: https://habr.com/ru/post/1674664/


All Articles