1

I have this 2d array in java

    String[][] Orders = new String[5000][7];

How do I create this in empty 2d array of strings in python?

I have tried

    Orders = [[]]

But when I go to put a value in it I get an index array out of bounds error

    Orders[0][0] = 'foo'
    Orders[0][1] = 'boo'
Dean-O
  • 1,143
  • 4
  • 18
  • 35
  • 2
    Highly likely that you won't need to initialize list if you'll use python in generic way. Pre-initialization is not required in 99% cases. Anyway, `orders = [[""] * 7 for _ in range(5000)]` – Olvin Roght May 18 '22 at 20:31
  • `Orders: list[list[str]] = []` The type hint is not necessary. – Eric Jin May 18 '22 at 20:31

1 Answers1

0
my_rows, my_cols = (3, 4)
my_array = [[0]*my_cols]*my_rows
print(my_array)

Link for reference https://pythonguides.com/create-an-empty-array-in-python/#:~:text=Create%20empty%20array%20Python,-In%20python%2C%20we&text=Create%20a%20list%20%5B0%5D%20and,will%20get%20an%20empty%20array.

edit: After further research multidimensional arrays do not seem as feasible in python as they are in Java

Here is a previous post on the topic: Two dimensional array in python

  • 1
    `my_array` will contain 4 references to same list of 3 zeros. Try to add `my_array[0][0] = 1` and print `my_array` again and you will see the problem. – Olvin Roght May 18 '22 at 20:39
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Sumit Sharma May 19 '22 at 03:58