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
• 6,670 points

7 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
+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]

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,730 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):

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
2 answers

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

  Tuple is a collection of Python objects.it is ...READ MORE

answered Apr 12 in Python by rajesh
• 1,210 points
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
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
• 10,780 points
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 1,400 points
+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
+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

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.