Kodeclik Blog

# Python Arrays versus Lists

## What are Lists and Arrays in Python?

Lists and arrays are both data structures in Python that are iterable, i.e., you can traverse the contents of the data structure element-by-element. They are useful whenever you want to model a sequence, i.e., wherever order is important, e.g., a sequence of bank transactions, a sequence of months, a sequence of house addresses, a sequence of college degrees, etc.

## How do you use lists in Python?

It is really very simple. You use square brackets to list your elements. Here’s below code to declare a list of dollar amounts and then print it.

```
a = [1,2,3,4]
print(a)
```

This produces the output:

`[1, 2, 3, 4]`

## How do you use arrays in Python?

Here is code to use arrays. Arrays do not come standard in Python and we need to import this functionality from a library such as numpy.

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

This will produce the same output:

`[1 2 3 4]`

(Note the absence of commas.)

## What are the similarities between Lists and Arrays in Python?

For a lot of purposes, you can write code that serves the same purpose using both arrays and lists.

## Similarity 1: Lists and arrays are both ordered.

This means that the order in which you store the elements is important and both preserve and respect the ordering. For instance, the following code

`print(a[0])`

prints the first element of “a” in both examples above. In both cases it will output 1.

## Similarity 2: Lists and arrays are both mutable.

Mutable is just a fancy way of saying you can change their contents after initializing them. (There are data structures that are immutable; for instance, strings are immutable. After initializing a variable to a string data structure, you cannot change it.)

Here is an example with arrays:

```
import numpy as np;
a = np.array([1,2,3,4])
a[0] = 5
print(a)
```

The output will be:

`[5 2 3 4]`

Here is an example with lists:

```
a = [1,2,3,4]
a[0] = 5
print(a)
```

The output will be:

`[5, 2, 3, 4]`

## Similarity 3: The syntax for lists and arrays is the same.

This is often the confusing part. If you hide the “import numpy as np” of the code below, and the invocation of np.array, you will have no way of knowing which is a list and which is an array.

## Similarity 4: Lists and arrays can both have duplicate elements.

In the example below, both the list and array contain duplicate elements.

Here is an example with lists:

```
a = [2,2,3,4]
print(a)
```

Here is an example with arrays:

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

## Similarity 5: Both lists and arrays can be sliced.

Just as we index into lists and arrays using indices beginning at 0, we can give a range of indices - the starting and the ending index plus 1 to get a slice of the list (or array).

Here’s an example with arrays:

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

This outputs:

`[2 3]`

Here is an example with lists:

```
a = [2,2,3,4]
print(a[1:3])
```

with the same output as above.

## What are some of the key differences between Lists and Arrays in Python?

The above discussion might suggest that lists and arrays are equivalent. But there are numerous differences between lists and arrays in Python. Here are some of the most important differences.

## Difference 1: Lists are part of core Python. Arrays are not part of core Python.

This means you have to import an array library (the most common is Numpy) to get the array functionality. Recall that in the code above, we had to import the array functionality from the numpy library above.

## Difference 2: Lists can contain elements of different types. An Array’s elements must all be of the same type.

Here’s an example of a list with elements of different types.

```
mylist = ['Kodeclik', 123, 'Washington, D.C.', True, 567.96, [3,4,5]]
print(mylist)
```

Note that our list, called mylist, contains strings, integers, floating point numbers, boolean, and even another list inside!

If we try to do the same with numpy:

```
import numpy as np;
y = np.array([3,4.5,'Hello'])
print(y)
```

The output is:

`['3' '4.5' 'Hello']`

It looked like it worked. But note that the numpy.array method when called with a heterogeneous collection of elements has converted all of them into strings. There is no way to create numpy arrays with a heterogeneous collection. You can have all elements to be numbers or all elements to be strings. For the code below:

```
import numpy as np;
x = np.array([3,4,5])
y = np.array(['hello','kodeclik'])
print(x)
print(y)
```

We get the output:

```
[3 4 5]
['hello' 'kodeclik']
```

## Difference 3: Lists don’t need to be declared. Arrays need to be declared before use.

In all of the above code segments, note that we use the array constructor to define the array before doing any further operations with the array.

## Difference 4: Arrays (in Numpy) are optimized for fast mathematical operations. Lists are not.

Consider:

```
import numpy
a = numpy.array([1, 2, 3])
b = numpy.array([4,5,6])
print(a+b)
```

This code does element-wise addition. This is not possible in lists. If you do:

```
a = [1,2,3]
b = [4,5,6]
print(a+b)
```

It works, but notice the output:

`[1, 2, 3, 4, 5, 6]`

## Difference 5: Arrays are optimized for storage (which is why you need to declare them before use). Lists are not.

Because arrays are declared with their full size upon initialization, arrays are optimized for storage. Lists, however, grow dynamically and therefore there is more “book-keeping” using pointers and thus Python will use more storage for lists.

## Difference 6: Lists can grow/shrink and are more flexible (they allow easy extension or reduction by adding/deleting elements). Arrays are not flexible.

Let us try growing a list first. Suppose we do:

```
x = [1,2]
x[2] = 3
print(x)
```

We will get:

```
x[2] = 3
IndexError: list assignment index out of range
```

To grow a list, you need to explicitly append to it. For instance:

`x.append(3)`

This gives:

`[1, 2, 3]`

Similarly, there is a delete method.

```
x = [1,2]
x.append(3)
del x[0]
print(x)
```

This yields:

`[2, 3]`

There are many other useful methods to add, delete, find, replace, and update elements.

Arrays, on the other hand, cannot be updated, extended. But there are useful append functions in libraries like numpy that will return a new array (not update the existing array). For instance:

```
import numpy
a = numpy.array([1, 2, 3])
newa = numpy.append(a, [4,5,6])
print(newa)
print(a)
```

This yields:

```
[1 2 3 4 5 6]
[1 2 3]
```

Note that the original array "a" is not modified.

## When should I use a List in Python?

Lists are more versatile because they can store elements of different types and can grow and shrink dynamically. So if you need to store dynamic, heterogeneous, collections use lists.

## When should I use an Array in Python?

In general if you are going to make heavy use of mathematical operations, or need to store and process a large amount of numerical data, you should go with arrays rather than lists. If you are also particular about efficient memory storage, you should use arrays.

Like to learn more about lists and arrays in Python? You will like our tutorials on how to copy Lists in Python and how Python sets work. Also learn what an index of -1 means in Python lists. See also our blogpost on Python's enumerate() capability and a more in-depth dive into empty Python lists. Finally, learn how to print Pascal's triangle in Python!

Want to learn Python with us? Sign up for 1:1 or small group classes.