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

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]

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

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 by sahil
• 580 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, 2019 in Python by Anjali
0 votes
1 answer

How to declare an array in Python?

variable = [] Now variable refers to an empty list*. Of ...READ MORE

answered Jan 5, 2021 in Python by Gitika
• 65,910 points
0 votes
1 answer
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,130 views
0 votes
1 answer
+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,440 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,440 points
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP