What is an array in Python How to declare it

+4 votes
I am unable to find arrays in the python documentation. How can I declare an array in Python?
Apr 12, 2018 in Python by ana1504.k
• 7,910 points
4,625 views

7 answers to this question.

+4 votes
Best answer

Python doesn't have a native array data structure, but it has the "list". In Python list, you can store elements of different types whereas in array all the elements should of the same type.

List is the most versatile datatype available in Python which can be written as a list of comma-separated values (items) between square brackets. Also in Python, there is a powerful N-dimension array object called as Numpy, which is in the form of rows and columns. You can initialize numpy arrays from nested Python lists and access it elements.

Consider the example below to declare a list:

Subjects = ['Physics', 'Chemistry', 'Maths', 2]
print(Subjects)

Notice that the Subjects List contains both words as well as numbers.

answered Apr 12, 2018 by anto.trigg4
• 3,440 points

selected Oct 12, 2018 by Omkar
+1 vote
variable = []

Now variable refers to an empty list*.

Of course this is an assignment, not a declaration. There's no way to say in Python "this variable should never refer to anything other than a list", since Python is dynamically typed.


*The default built-in Python type is called a list, not an array. It is an ordered container of arbitrary length that can hold a heterogenous collection of objects (their types do not matter and can be freely mixed). This should not be confused with the array module, which offers a type closer to the C array type; the contents must be homogenous (all of the same type), but the length is still dynamic.

answered Oct 12, 2018 by findingbugs
• 4,780 points
+1 vote

You don't actually declare things, but this is how you create an array in Python:

from array import array
intarray = array('i')

For more info see the array module: http://docs.python.org/library/array.html

Now possible you don't want an array, but a list, but others have answered that already. :)

answered Oct 12, 2018 by abc
+1 vote

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.

answered Oct 12, 2018 by rani
+1 vote

This is how:

my_array = [1, 'rebecca', 'allard', 15]
answered Oct 12, 2018 by kalpesh
+1 vote

Python calls them lists. You can write a list literal with square brackets and commas:

>>> [6,28,496,8128]
[6, 28, 496, 8128]
answered Oct 12, 2018 by Progba
0 votes
Arrays are popular in most programming languages like: Java, C/C++, JavaScript etc. In python not Array Concept. Here We Discuss About Python List.We can treat lists as arrays. However, we cannot constrain the type of elements stored in a list. For example:

           x= [1, 2, "Hello"]
answered Apr 10, 2019 by rajesh
• 1,270 points

Related Questions In Python

0 votes
4 answers

What is a Tuple in Python and how to use it?

Tuples  are a  Unchanging sequence of values, ...READ MORE

answered Jun 21, 2020 in Python