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.
This produces the output:

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.
This will produce the same output:
(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
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:
The output will be:
Here is an example with lists:
The output will be:

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:
Here is an example with arrays:

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:
This outputs:
Here is an example with lists:
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.
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:
The output is:
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:
We get the output:

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:
This code does element-wise addition. This is not possible in lists. If you do:
It works, but notice the output:

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:
We will get:
To grow a list, you need to explicitly append to it. For instance:
This gives:
Similarly, there is a delete method.
This yields:
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:
This yields:
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.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.
  • ABOUT

Copyright @ Kodeclik 2023. All rights reserved.