Although identical syntax, semantically what is produced with it is very different in each language.
In Pyhton this code actually produces a list. In Javascript what you are creating is a dictionary, also known as a map or array associative.
In Python there is another syntax to create a dictionary.
In Javascript there is an optimization to treat a dictionary as array, whenever possible. Actually this is the term used, but it looks more like a list than a array.
A array or a list is characterized by having the elements in sequence and densely, being able to access them with complexity O(1), that is, any access is made in an operation of direct form.
A dictionary is sparse and has no order in the elements. Despite accessing each element on O(1), it is not direct, it is necessary to calculate where the key is, since it has no order and may not exist.
A dictionary usually creates a new key automatically when it attempts to place a value on a non-existent key. When doing this will probably inhibit optimizations to treat the dictionary as a list, but depends on implementation.
As in Python there is this confusion of concepts in the same syntax needs to be explicit of what you want.
If you can use a list, use it, even if you have to adapt the code a bit. If you really need to create a dictionary, then go for it. But don’t create the dictionary just to look like JS or just to write seemingly simpler code. A list is always more advantageous than a dictionary if the elements are used in order with complete sequence of numbers.
Eventually even when it looks like a dictionary it may be more advantageous for performance and memory to use a list in place of the dictionary even when some elements are missing.
I don’t see how to do that if I don’t initialize the list to the required size, but mostly I don’t see any reason to do that. Depending on your need, it may be more interesting to use a dictionary. What is the purpose?
– Woss
How to set the size?
– Morais Vilson
Can you describe what you want to do? There is a real need to inform the key when storing a value?
– Woss
Actually thinking about it I think I don’t need to know the key, I just want to create a list of teams that is a list of players that are objects.
– Morais Vilson
I can use a normal for printing all objects from a list or there is a more appropriate method?
– Morais Vilson
To add is by
append
same. Yes, go through the list for afor
.– Woss
And if I have a list within a list I should use two for or there’s another way?
– Morais Vilson
dictionary keys can contain spaces?
– Morais Vilson
About dictionary keys: you better ask another question. But in short - if they are a string, they can contain spaces and any character. But in your case it might be more interesting to use "tuples": comma-separated numbers - for your dictionary keys. This is a simple way to work with Python multidimensional arrays.
– jsbueno