Differences between Python and Javascript in relation to arrays?

Asked

Viewed 357 times

4

I’m having some difficulty because in Javascript it was possible to do this:

const x = []
x[0] = 1
x[1] = 2

But in Python when I create an empty list and try to make a statement it says that the index is out of range. I can get around with the method append() but wanted to know if you have a way to do as in Javascript.

  • 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?

  • How to set the size?

  • Can you describe what you want to do? There is a real need to inform the key when storing a value?

  • 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.

  • I can use a normal for printing all objects from a list or there is a more appropriate method?

  • To add is by append same. Yes, go through the list for a for.

  • And if I have a list within a list I should use two for or there’s another way?

  • dictionary keys can contain spaces?

  • 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.

Show 4 more comments

1 answer

7


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.

  • 1

    Complement only: to create a list with a predefined size in Python, one way is to create a list containing only one immutable element (in general None or 0), and multiply by the desired size. "Multiplication" by an integer N is defined for Python sequences as "concatenating the sequence with itself N times": x = [0,] * 5 creates a list of 5 pre-allocated elements. it is important not to use this to create lists inside lists when you want to create a 2D matrix - (internal lists will be clones to each other). If necessary, the indication is to use Numpy.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.