0

I want to create a 2d vector in python and later add elements to it accordingly. I should also be able to retrieve the size of the vector in my code.

1

1 Answer 1

2

Use list of lists.

myL = []
for i in range(5):
   myL.append([i for i in range(5)])

for vector in myL:
    print(vector)

Output:

[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]
[0, 1, 2, 3, 4]

For every element of the list myL, you can get the length using len(myL[index]), and can also append element to it using myL[index].append(newelement). Example:

print(len(myL[2]))
# prints 5
myL[2].append(100)
print(len(myL[2]))
# prints 6
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.