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 11, 2018 in Python by ana1504.k
• 7,890 points
271 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 11, 2018 by anto.trigg4
• 3,420 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,750 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 by rajesh
• 1,210 points

Related Questions In Python

0 votes
3 answers
0 votes
1 answer

Is it possible to create an array with all values as zero in python?

You can use  np.zeros(4,3) This will create a 4 ...READ MORE

answered May 24 in Python by Anjali
57 views
0 votes
1 answer
0 votes
1 answer

Is arr.__len__() the preferred way to get the length of an array in Python?

my_list = [1,2,3,4,5,6,7] len(my_list) # 7 The same works for ...READ MORE

answered Oct 8, 2018 in Python by SDeb
• 13,190 points
74 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 346 views
+2 votes
3 answers

what is the practical use of polymorphism in Python?

Polymorphism is the ability to present the ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,420 points
1,600 views
+2 votes
2 answers

How can I create a new file in Python?

You can try the below code which ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,420 points
121 views