person writing bucket list on book
Photo by Glenn Carstens-Peters on Unsplash

When you create a new array in Java, you can set up the length of it. If you are programming mostly in Python these days, then you may not be aware that you can also do that in Python.

Let’s say that we have a fixed number of students and we want to save their heights in a list.

We can of course just declare an empty list and save their heights by appending new elements to the empty list. 

However, there can be cases when you may need to have a fixed length and not want to allow additional elements to be added to it. Or maybe you know that you always have the same number of elements.

You may also need to have a default value for each element.

If that is the case, then you can consider using the following technique:

Here is the complete example:


That’s it for this simple trick that you probably knew already but haven’t thought of using it in such a context. I hope you find it useful.