0

In Java, we make an array of objects with a fixed size as follows

ArrayClass array_of_objects[]=new ArrayClass[10];

but how can I make a list of objects in python with a fixed size?

0

2 Answers 2

2

Create an empty list of size 5

lst = [None] * 5
Sign up to request clarification or add additional context in comments.

5 Comments

this makes a list, but OP wanted an array. They are not the same, but very similar
@EmiOB I don't think that, coming from Java, the OP knows the difference, and most likely, they mean list. (Especially, considering the last sentence.)
I also think :D
This doesn't have a fixed size, though
@bereal fair point. Just thought it was important to note that they are two different things in python :)
2

use numpy like below:

import numpy as np
size = 2
np.array([None]*size, dtype=object)

Output:

array([None, None], dtype=object)

you can use this like below:

arr = np.array([None]*2, dtype=object)
arr[0] = 1
arr[1] = 'str'

Output:

array([1, 'str'], dtype=object)

2 Comments

Numpy seems like overkill for the question asked
@OneCricketeer hahahah:)))

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.