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'
2
  • 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)] Commented May 18, 2022 at 20:31
  • Orders: list[list[str]] = [] The type hint is not necessary. Commented May 18, 2022 at 20:31

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.