613

How do I declare an array in Python?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
  • 47
    @Glenn Maynard: probably because in C-like languages arrays are fixed length while Python lists are not. Its more like STL vector in C++ or ArrayList in Java. – MAK Oct 03 '09 at 19:36
  • 137
    It's called a list, because it's a list. [A(), 1, 'Foo', u'öäöäö', 67L, 5.6]. A list. An array is "an arrangement of items at equally spaced addresses in computer memory" (wikipedia). – Lennart Regebro Oct 03 '09 at 19:40
  • 3
    Also, lists can have heterogeneous contents, while other languages' arrays typically require all elements to be of the same type. – Ned Batchelder Oct 03 '09 at 19:40
  • 39
    Nothing about the universally-understood term "array" suggests a fixed length or anything about the content; those are just limitations of C's particular implementation of arrays. Python lists are equally spaced (pointers to objects, internally), or else `__getitem__` wouldn't be O(1). – Glenn Maynard Oct 03 '09 at 20:13
  • 2
    Duplicate: http://stackoverflow.com/questions/856948/arrays-in-python – S.Lott Oct 03 '09 at 20:34
  • 28
    @Glenn, from en.wikipedia.org/wiki/Array_data_structure : "the elements of an array data structure are required to have the same size" (true for Python's arrays, not true for Python lists) and "set of valid index tuples and the addresses of the elements (and hence the element addressing formula) are usually fixed while the array is in use" (not true in Python for either list or array). – Alex Martelli Oct 03 '09 at 22:27

17 Answers17

413
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.

Zanon
  • 29,231
  • 20
  • 113
  • 126
sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • 15
    Would it be possible to initialize the contents of the array, as in JavaScript? (e.g., as `variable = ["Hi", "Hello"];`?) – Anderson Green Mar 18 '13 at 04:31
  • 3
    How would you declare a multidimensional array, then (e. g., a 2D array?) – Anderson Green Apr 05 '13 at 01:45
  • 8
    @AndersonGreen As I said there's no such thing as a variable declaration in Python. You would create a multidimensional list by taking an empty list and putting other lists inside it or, if the dimensions of the list are known at write-time, you could just write it as a literal like this: `my_2x2_list = [[a, b], [c, d]]`. Depending on what you need multi-dimensional arrays for, you also might consider using `numpy`, which defines array types for multi-dimensional, homogeneous, unboxed arrays that can be much more efficient where applicable, so they're preferable for numeric computations. – sepp2k Apr 05 '13 at 09:43
  • Can I define array like the following: stats = {} – Tan May 07 '13 at 03:49
  • @AaK No, `{}` is an empty dictionary, not a list. – sepp2k May 07 '13 at 11:23
  • can we access `list` like this `variable[0]`? – Ifan Iqbal Oct 09 '13 at 23:30
  • 1
    @IfanIqbal Yes, if it contains at least one element, you can. – sepp2k Oct 09 '13 at 23:34
  • 1
    It is formally known as list – Arulx Z Nov 10 '15 at 04:07
  • 1
    I've updated this answer to make it clear that this covers *lists*, and that there is a *very separate concept* of arrays in Python too. This question and answer are a source of confusion for newcomers because this information was lacking here. – Martijn Pieters Feb 29 '16 at 16:14
  • 1
    Lists are not arrays – Mina Gabriel Oct 27 '16 at 21:21
  • 1
    @MinaGabriel They're arrays in the sense that most non-Python people, including the OP, use that word. That is, the OP didn't use the term "array" because he wanted the special-purpose unboxed data structure defined in the `array` module. He used the term "array" because he didn't realize that Python calls its "normal array" data structure "list". – sepp2k Oct 27 '16 at 22:15
  • @sepp2k The array module interface the Python List class, this means it has some more advantage, Array in python storing the bits that represent the pre-defined data, Lists are referential structures, there are many advantages of arrays over Lists in terms of computing performance, as the highest voted answer you should explain this a little bit more. – Mina Gabriel Oct 28 '16 at 10:46
  • I disagree with this answer. If you want to assign values to particular slots in your list, you must first initialize all the elements of your "fixed size array". So `a=[]` won't help you. A Python dict is actually closer in behavior. It is technically an Associative Array. You can assign values to the 'array' slots at will without explicitly initializing all the slots. You could use a `defaultdict` with a default value of 0 to get identical behavior to a global array in C (minus array overruns), although the implementation internally is quite different (as a user, you probably don't care). – BobDoolittle Jan 05 '17 at 23:34
  • what happens if I declare it as, variable = list() ?? – wolfsbane Jun 10 '17 at 15:51
  • how is ini_me=list() instead of ini_me=[] – Timo Dec 31 '20 at 18:54
189

This is surprisingly complex topic in Python.

Practical answer

Arrays are represented by class list (see reference and do not mix them with generators).

Check out usage examples:

# empty array
arr = [] 

# init with values (can contain mixed types)
arr = [1, "eels"]

# get item by index (can be negative to access end of array)
arr = [1, 2, 3, 4, 5, 6]
arr[0]  # 1
arr[-1] # 6

# get length
length = len(arr)

# supports append and insert
arr.append(8)
arr.insert(6, 7)

Theoretical answer

Under the hood Python's list is a wrapper for a real array which contains references to items. Also, underlying array is created with some extra space.

Consequences of this are:

  • random access is really cheap (arr[6653] is same to arr[0])
  • append operation is 'for free' while some extra space
  • insert operation is expensive

Check this awesome table of operations complexity.

Also, please see this picture, where I've tried to show most important differences between array, array of references and linked list: arrays, arrays everywhere

Anton
  • 10,890
  • 8
  • 45
  • 54
  • 6
    Just to add, there is a really cool way to cut arrays in python: for `[1, 2, 3, 4, 5, 6, 7, 8, 9][1:-2]` result will be `[2, 3, 4, 5, 6, 7]` – Anton Mar 16 '16 at 17:19
  • 3
    I see some down votes for this post time to time. It would be great if someone could post a word why. Thanks. – Anton Oct 31 '16 at 12:29
  • 4
    You da real MVP. Need to know actual chosen design of 'list' in order to make reasonable programming decisions. Basically it's like a 'vector' in C++. Thanks! – Necro Nov 06 '17 at 00:15
  • May I add the practical notation for comparison, for instance: `a == b[:2]` returns True if the first 2 elements of _b_ equals the values of array _a_ – Bruno L. May 17 '20 at 10:53
140

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

Nicholas Riley
  • 43,532
  • 6
  • 101
  • 124
Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
  • 12
    This is sort of funny, but not really a good answer for a question tagged "beginner". Just to make it clear: In Python you usually use a data type called a `list`. Python has a special-purpose data type called an `array` which is more like a C array and is little used. – steveha Oct 04 '09 at 02:48
  • 71
    No, but everyone else already used a list. I thought it would be a good answer to point out that there are arrays too. – Lennart Regebro Oct 04 '09 at 07:38
  • 11
    super mad props for this answer. I've been programming in Python for years and only recently realized that there was an actual Python array object that is different from list objects. While the data struct is very similar, arrays limit what type of objects the array can hold. Great answer @LennartRegebro! – Josh Brown Jul 04 '15 at 10:38
  • 6
    This should be the right answer List and Arrays are two different things @LennartRegebro thanks – Mina Gabriel Oct 28 '16 at 10:48
  • 1
    There's a specific reason you may want to use arrays instead of lists. Specifically, if you have an array called myarray full of numbers, you can perform mathematical operations against it, and those operations are applied to all items inside of it. So, if you execute myarray/3, every number inside gets divided by 3. If you attempt to do the same thing with a list, you will get an error. So, arrays are more efficient for large datasets of numbers. – Matthew Jul 14 '19 at 02:30
  • 1
    The array module is very limited to int and floats only (Unicode is deprecated). That's why it's not known to many Python developers. Read more at https://www.askpython.com/python/array/python-array-examples – Pankaj Sep 05 '19 at 07:47
67

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

limitcracker
  • 2,208
  • 3
  • 24
  • 23
38

This is how:

my_array = [1, 'rebecca', 'allard', 15]
pradyunsg
  • 18,287
  • 11
  • 43
  • 96
canadiancreed
  • 1,966
  • 6
  • 41
  • 58
  • 4
    Since this is not a python array but python list, would it not be less confusing to call it "my_list"? – Mattis Asp Oct 31 '17 at 15:23
  • 5
    That creates a list, not an array. They are different and have different properties in python. Specifically, you can perform numerical operations against an array, but not against a list. – Matthew Jul 14 '19 at 02:32
  • 1
    This statement does not answer the question posted. – Rick Henderson Dec 22 '21 at 16:28
26

For calculations, use numpy arrays like this:

import numpy as np

a = np.ones((3,2))        # a 2D array with 3 rows, 2 columns, filled with ones
b = np.array([1,2,3])     # a 1D array initialised using a list [1,2,3]
c = np.linspace(2,3,100)  # an array with 100 points beteen (and including) 2 and 3

print(a*1.5)  # all elements of a times 1.5
print(a.T+b)  # b added to the transpose of a

these numpy arrays can be saved and loaded from disk (even compressed) and complex calculations with large amounts of elements are C-like fast.

Much used in scientific environments. See here for more.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Remi
  • 20,619
  • 8
  • 57
  • 41
25

JohnMachin's comment should be the real answer. All the other answers are just workarounds in my opinion! So:

array=[0]*element_count
csabinho
  • 1,579
  • 1
  • 18
  • 28
  • 3
    agree. except careful to call even the variable "array", or get purist wrath. to this i would also add that you can similarly create "multidimensional arrays": `x=[[0] * 10] * 10` – Peter S Magnusson May 30 '20 at 02:29
  • Yes, this is still a list. Use the type() function to determine for your self. – Rick Henderson Dec 22 '21 at 16:31
  • 1
    @PeterSMagnusson you should particularly **not** do that for a multidimensional array. What will happen is that each row will be the same, because they're all references to the same object. Try this out for example: `x = [[0]*5]*5; x[1][2] = 1; print(x)` – Johan Nov 30 '22 at 10:59
  • @johan is right of course! the fix is: `import copy; x = [copy.copy(x) for x in [[0]*5]*5]; x[0] is x[1]`. – Peter S Magnusson Jan 06 '23 at 18:49
22

A couple of contributions suggested that arrays in python are represented by lists. This is incorrect. Python has an independent implementation of array() in the standard library module array "array.array()" hence it is incorrect to confuse the two. Lists are lists in python so be careful with the nomenclature used.

list_01 = [4, 6.2, 7-2j, 'flo', 'cro']

list_01
Out[85]: [4, 6.2, (7-2j), 'flo', 'cro']

There is one very important difference between list and array.array(). While both of these objects are ordered sequences, array.array() is an ordered homogeneous sequences whereas a list is a non-homogeneous sequence.

hussam
  • 830
  • 9
  • 17
17

You don't declare anything in Python. You just use it. I recommend you start out with something like http://diveintopython.net.

bayer
  • 6,854
  • 24
  • 35
  • 2
    Some times you must declare the type of a variable: if you don't use it before, a control structure, it doesn't exist outside the control structure and you will then be constructing a new variable. The assumption is then that the variable is an int, which clashes if you use it as a more complex type. – Clearer Feb 19 '15 at 11:31
  • @Clearer yes, using functions sometimes need to declare it, and sometimes to play some globals when using functions and don't wanna write too much arguments on functions. – m3nda Jun 12 '15 at 16:13
  • It's not just function; a simple if statement could give you the same problem. – Clearer Jul 29 '15 at 12:04
  • 2
    Programming is all about declaring, no matter which language do you use. Type declaring is whole different story – Outside_Box Aug 15 '18 at 16:08
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – ClimateUnboxed Dec 04 '19 at 21:02
  • @bayer The link is dead. – Mehdi Charife May 29 '23 at 15:48
13

I would normally just do a = [1,2,3] which is actually a list but for arrays look at this formal definition

non sequitor
  • 18,296
  • 9
  • 45
  • 64
12

To add to Lennart's answer, an array may be created like this:

from array import array
float_array = array("f",values)

where values can take the form of a tuple, list, or np.array, but not array:

values = [1,2,3]
values = (1,2,3)
values = np.array([1,2,3],'f')
# 'i' will work here too, but if array is 'i' then values have to be int
wrong_values = array('f',[1,2,3])
# TypeError: 'array.array' object is not callable

and the output will still be the same:

print(float_array)
print(float_array[1])
print(isinstance(float_array[1],float))

# array('f', [1.0, 2.0, 3.0])
# 2.0
# True

Most methods for list work with array as well, common ones being pop(), extend(), and append().

Judging from the answers and comments, it appears that the array data structure isn't that popular. I like it though, the same way as one might prefer a tuple over a list.

The array structure has stricter rules than a list or np.array, and this can reduce errors and make debugging easier, especially when working with numerical data.

Attempts to insert/append a float to an int array will throw a TypeError:

values = [1,2,3]
int_array = array("i",values)
int_array.append(float(1))
# or int_array.extend([float(1)])

# TypeError: integer argument expected, got float

Keeping values which are meant to be integers (e.g. list of indices) in the array form may therefore prevent a "TypeError: list indices must be integers, not float", since arrays can be iterated over, similar to np.array and lists:

int_array = array('i',[1,2,3])
data = [11,22,33,44,55]
sample = []
for i in int_array:
    sample.append(data[i])

Annoyingly, appending an int to a float array will cause the int to become a float, without throwing an exception.

np.array retain the same data type for its entries too, but instead of giving an error it will change its data type to fit new entries (usually to double or str):

import numpy as np
numpy_int_array = np.array([1,2,3],'i')
for i in numpy_int_array:
    print(type(i))
    # <class 'numpy.int32'>
numpy_int_array_2 = np.append(numpy_int_array,int(1))
# still <class 'numpy.int32'>
numpy_float_array = np.append(numpy_int_array,float(1))
# <class 'numpy.float64'> for all values
numpy_str_array = np.append(numpy_int_array,"1")
# <class 'numpy.str_'> for all values
data = [11,22,33,44,55]
sample = []
for i in numpy_int_array_2:
    sample.append(data[i])
    # no problem here, but TypeError for the other two

This is true during assignment as well. If the data type is specified, np.array will, wherever possible, transform the entries to that data type:

int_numpy_array = np.array([1,2,float(3)],'i')
# 3 becomes an int
int_numpy_array_2 = np.array([1,2,3.9],'i')
# 3.9 gets truncated to 3 (same as int(3.9))
invalid_array = np.array([1,2,"string"],'i')
# ValueError: invalid literal for int() with base 10: 'string'
# Same error as int('string')
str_numpy_array = np.array([1,2,3],'str')
print(str_numpy_array)
print([type(i) for i in str_numpy_array])
# ['1' '2' '3']
# <class 'numpy.str_'>

or, in essence:

data = [1.2,3.4,5.6]
list_1 = np.array(data,'i').tolist()
list_2 = [int(i) for i in data]
print(list_1 == list_2)
# True

while array will simply give:

invalid_array = array([1,2,3.9],'i')
# TypeError: integer argument expected, got float

Because of this, it is not a good idea to use np.array for type-specific commands. The array structure is useful here. list preserves the data type of the values.

And for something I find rather pesky: the data type is specified as the first argument in array(), but (usually) the second in np.array(). :|

The relation to C is referred to here: Python List vs. Array - when to use?

Have fun exploring!

Note: The typed and rather strict nature of array leans more towards C rather than Python, and by design Python does not have many type-specific constraints in its functions. Its unpopularity also creates a positive feedback in collaborative work, and replacing it mostly involves an additional [int(x) for x in file]. It is therefore entirely viable and reasonable to ignore the existence of array. It shouldn't hinder most of us in any way. :D

Community
  • 1
  • 1
Melvin
  • 1,530
  • 11
  • 18
8

How about this...

>>> a = range(12)
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
>>> a[7]
6
Arif Amirani
  • 26,265
  • 3
  • 33
  • 30
slehar
  • 97
  • 1
  • 1
7

Following on from Lennart, there's also numpy which implements homogeneous multi-dimensional arrays.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
camh
  • 40,988
  • 13
  • 62
  • 70
6

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

>>> [6,28,496,8128]
[6, 28, 496, 8128]
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
4

I had an array of strings and needed an array of the same length of booleans initiated to True. This is what I did

strs = ["Hi","Bye"] 
bools = [ True for s in strs ]
  • I am here because I wanted the C declaration: `int count[26]={0};` There is probably a better way but this variant of `bools` from above worked `count=[0 for ii in range(26)]` Later, I then changed it to `count=[0]*26` which seems preferable. – user1683793 Dec 21 '18 at 23:14
3

You can create lists and convert them into arrays or you can create array using numpy module. Below are few examples to illustrate the same. Numpy also makes it easier to work with multi-dimensional arrays.

import numpy as np
a = np.array([1, 2, 3, 4])

#For custom inputs
a = np.array([int(x) for x in input().split()])

You can also reshape this array into a 2X2 matrix using reshape function which takes in input as the dimensions of the matrix.

mat = a.reshape(2, 2)
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Amit Prafulla
  • 381
  • 2
  • 5
2
# 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.

tonyb
  • 379
  • 2
  • 6