How do I declare an array in Python?

Viewed 2327757

How do I declare an array in Python?

17 Answers

I think you (meant)want an list with the first 30 cells already filled. So

   f = []

   for i in range(30):
       f.append(0)

An example to where this could be used is in Fibonacci sequence. See problem 2 in Project Euler

# This creates a list of 5000 zeros
a = [0] * 5000  

You can read and write to any element in this list with a[n] notation in the same as you would with an array.

It does seem to have the same random access performance as an array. I cannot say how it allocates memory because it also supports a mix of different types including strings and objects if you need it to.

Related