0

I am writing a code and I want to assign a 2D array in python.

The algorithm for my code is

For i = 0 To n
                A(i, 0) = 0
            Next i

How can I convert the above code into python ?

If it was a 1d array then I could use

A = [0 for i in range(int(n))]

However, for the 2d array I am not getting any clue. I am using python 3.3.

Thanks for your attention.

Jdbaba

1 Answer 1

4

Nest them.

[[0 for i in ...] for j in ...]
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much for your reply. I used as per your suggestion like this : A = [[0 for i in range(int(n))]for j in 0]. I got the error "Type error 'int' is not iterable".
@Jdbaba for j in 0 is the problematic part, 0 is an integer which is not iterable like lists, tuples, strings and dictionaries(and generators too), change that to something like for j in range(number_of_rows)

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.