Where are the Arrays, Python?

Understanding why “arrays” look and sound so weird in this language

Billy Joe Ramos
5 min readSep 26, 2021
A generic Python image that can be found on internet

Recently I went back to study Python, my first and, for a couple months, favorite programming language and faced a teenie-tiny concept that got my mind shaken. Where are the arrays? Most languages have this structure well defined, despite defining sizes or types (what py doesn’t by the way, but more on that later), so why the heck they are called lists? Since I’m no bozo, decided to find out! Come with me in this crazy adventure.

“BUT WHAT ARE ARRAYS, MR. BJ?”, you might ask. Okay, I’ll help you, but next time go research, huh?

Graphical representation of an array

“Ignoring size an array may be seen as an abstract data type with the operations new(), set(i, v, A), and get(i, A), where i is a numeric index, v is a value, and A is an array. The operations may be defined with axiomatic semantics as follows.”

According to NIST Dictionary of Algorithms and Data Structures, started in 1998 by Paul E. Black, Ph.D.

Still complicated to understand? To technical? OK, for mere mortals like me that get confused by fancy words in fat books: Let’s say your mother gave you some money and a shopping-list for the grocery. You can buy the items she wrote in the list, but can’t buy more than that because there’s only money for that. You can also buy less than what it was intended to, but the the money is already your mother’s, so it makes no sense to save it. There is no possibility to buy nothing that doesn’t sell in a grocery store and spend all your money in stickers for your album. The money with you only for the shopping.

Replace grocery items for data types and the list items by an integer-numeric length and you have an array; Fixed size and defined types. In this article we’ll focus on why Python decided to take this approach, maybe some other day I’ll write something more detailed about data structures.

Basic array usage example in C:

#include <stdio.h>
int main()
{
int x=0;

/* Array- declaration – length 4*/
int num[4];

for (x=0; x<4;x++)
{
printf("Enter number %d \n", (x+1));
scanf("%d", &num[x]);
}

return 0;
}

Back to the main subject. Despite being a useful guide to lists methods and helping expand your creativity on what can be done with arrays / lists, Python’s documentation doesn’t explain clearly why that naming problem exists¹. Well, the direct answer is quite simple: There are no arrays natively in Python. Or import’em, or don’t use’em. Lists may look like arrays, but they are not; According to the GNU C Manual, for declaring an array you have to specify the data type for its elements, its name, and the number of elements it can store.

“Specify type? Specify size? That doesn’t look very pythonic to me!” That’s right, my friend. Python lists are based on another data structure called… List (A.K.A. indexed list or liked list).

“Oh, another complicated data structure for me to learn? I’m so sick of it…” Sorry, mate. If you work with Software / Computer you’ll have to learn new things each and every day to survive. But good news is that lists are not that hard to understand, they are a flexible dynamic data structure, allowing to write robust programs which require much less maintenance, according to Auckland University’s material reference about Algorithms and Data Structures. Lists are like really long lines of people waiting in a bank to pay their bills or complain about the IRS. This line can grow and grow independent of the initial size, and all sorts of people can get in the line. That’s it, but again replace people by computer data.²

Comparing arrays (left) and linked lists (right)

In Python, lists are so powerful structures in that you can use them like arrays (limiting the size and allowing only one datatype to come in), vectors, regular lists with no restriction other than your computer’s memory, queues and stacks, with a little help from m̶y̶ ̶f̶r̶i̶e̶n̶d̶s̶ the official documentation. Hope you enjoyed this article!

“Hang on a sec, mate! You told me exactly “Or import’em, or don’t use’em”. How can I import and use this array type just like C or Java?³

Well, GeeksforGeeks published a useful and practical tutorial that I’ll link here. This is a example from their webpage, but try accessing it, read it and understand, not just copy.

# Python program to demonstrate# Creation of Array# importing "array" for array creationsimport array as arr# creating an array with integer typea = arr.array('i', [1, 2, 3])# printing original arrayprint ("The new created array is : ", end =" ")for i in range (0, 3):    print (a[i], end =" ")print()# creating an array with float typeb = arr.array('d', [2.5, 3.2, 3.3])# printing original arrayprint ("The new created array is : ", end =" ")for i in range (0, 3):    print (b[i], end =" ")The new created array is :  1 2 3 
The new created array is : 2.5 3.2 3.3

Ready! Now you can easily understand how there are no arrays in python and why they chose this approach developing the language!

[1]: Little side note here: It’s a healthy practice for your career and your brain to read the documentation, so open first the oldest Python documentation you can find (in my case 2.7) and the most recent, to track major updates and “initial” purpose of that structure / type.

[2]: I recommend this article on medium explaining better the differences

[3]: In case you’re wondering, in JavaScript arrays are more like lists than arrays

References:

Python 2.7 Documentation: https://docs.python.org/2.7/tutorial/datastructures.html

Python 3 Documentation: https://docs.python.org/3/tutorial/datastructures.html

NIST Dictionary of Algorithms and Data Structures: https://xlinux.nist.gov/dads/

Dr. Paul E. Black Bio: https://www.nist.gov/people/paul-e-black

Arrays, by NIST: https://xlinux.nist.gov/dads/HTML/array.html

Arrays, by C: https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Arrays

Arrays, by Auckland University: https://www.cs.auckland.ac.nz/software/AlgAnim/lists.html

Arrays in C: https://beginnersbook.com/2014/01/c-arrays-example/

Linked Lists x Arrays: https://medium.com/@yk392/what-is-a-linked-list-linked-list-vs-array-92f0db4015cc

--

--

Billy Joe Ramos

Christian, Software Development and Engineering student with main projects focused in web platforms, also passionate about literature and music.