Python Array Explained and Visualized

stylized artwork with Python arrays

Dividing and organizing information

In previous installments of our “Python Explained” series, we examined a few neat Python functions — as it turned out, each seemingly simple function (e.g. range()) has some interesting and quirky behaviors that you should be prepared for. Of course, Python has a lot of other topics to explore — and in this article, we’re taking a look at Python arrays.

In this tutorial, we’ll delve into how Python arrays work, what their limitations are, and how you can use them to maximize their efficiency. Upon reading this post, see some other Python articles that you might find interesting (and check our Python interview questions out):

So What Is an Array in Computer Science?

An array is a data structure which is a format that specifies how data should be organized, managed, and stored in order to be accessed and modified efficiently. Typically, we use an array to represent a basic value: characters, integers, or floating-point numbers. We’ve covered various data structures and their importance in a recent article titled Overview of Basic Data Structures: How to Organize Data the Efficient Way, so you can use it to brush up on this topic.

So How Is an Array Organized?

In this data structure, elements are stored in a specific order in a contiguous memory block. Here’s a neat little visualization:

visualization of how Python arrays function

How Can Array Elements Be Accessed?

Let’s take an array A of size N. A unique index ‘i’ is given to each memory location. This index is usually referenced as A[i] (another notation is Ai).

‘C’, ‘L’, ‘A’, ‘H’, ‘S’, and ‘E’ are all data values. It should be noted that they must usually be of the same type, but different programming languages enforce different rules, with some of them allowing for “multi-type” arrays.

What Are the Potential Downsides of Using an Array?

The inability to mix data types inside an array can be a major problem — for one, it really tests your attentiveness; it is also a suboptimal design decision even if some programming languages actually allow you to mix them.

In C-like languages, there’s another problem that has to do with the fact that the array size cannot change. Let’s say we’re creating an array which with various operating systems: there’s Windows, there’s Linux… and so we naively create an array for two values: int x[2]. But wait — we forgot macOS! In this case, in C-like languages, the only option that would allow us to add the third element is creating a new array and copying the data values from the old array to the new one. Phew!

Working with a Python Array

Arrays are an essential part of many programming languages, but they aren’t exactly native to Python. To use an array in Python, we first have to import the array module:

This little detail shows that, in Python, the array functionality can be (partially) substituted by some built-in functions like list. However, as we’ll learn in the sections below, arrays and lists are far from the same thing. Upon importing the array module, we can finally use the array function to initialize a new array. Now we can analyze its syntax.

Syntax and Arguments

The syntax isn’t straightforward — it introduces a couple of terms we need to define in order to actually understand the arrays:

So what do these terms mean? The typecode, as the name suggests, defines the object’s type of code, i.e. whether the array should contain values of Type A, Type B, Type C, etc. The provides the complete list of the typecodes you can use:

Python and C typecodes
As we can see from the table head titled “Python Type”, we can use the following data types in our arrays:

  1. Integers,
  2. Floating-point numbers,
  3. Unicode characters (however, the documentation warns that this typecode is deprecated since version 3.3 and will be removed in version 4.0)

If you feel like brushing up on the C types, please refer to the following article: C data types.

The initializer, on the other hand, is an optional argument that defines which elements should be included in the array. These elements (their type, to be precise) have to comply with the timecode; otherwise, you get a TypeError:

This will output:

Python Array Methods and Operations

Like with any other data structure, we can apply a plethora of methods to the array — some typical operations include counting, appending, and removing the elements.

Appending an Element

To append a new item x to the end of the array, we can use array.append(x):

This will output array('i', [1, 2, 3, 4, 5, 6]).

Counting an Element

To find how frequent the item x occurs in the array, we can use array.count(x):

This will output 3.

Extending an Array

We can use an iterable’s items to extend our array; if you need to brush up on iterables and iterators, please refer to our article titled Python zip() Function Explained and Visualized. The iterable we use can be either: a) an array or b) non-array.

Scenario 1: Extending an array with another array. In this case, the two arrays must have the same typecode — we run into TypeError otherwise.

This will output array('i', [1, 2, 3, 4, 5, 6]).

Scenario 2: Extending an array with any other iterable other than the array. Once again, the elements have to be of the same type:

The output will be the same as in Scenario 1.

Finding the Element’s First Occurrence

visualization of how Python array methods and functions work

array.index(x) allows us to obtain the index of the element x when it first occurred:

This will output 0 and 7 respectively.

Inserting an Element

With array.insert(i, x), we can insert a new item x in the array before position i.

This will output array('i', [1, 2, 3]). We can also use negative values — they are treated as being relative to the array’s end. For instance, to insert a new second-to-last item, we use -1 as the value for i:

This will output array('i', [1, 2, 3, 4, 5]).

Removing an Element

For this operation, we have two methods at our disposal.

Use array.pop([i]) to remove the item by its index: pop() takes the optional argument [i], deletes, and returns the corresponding item. If no argument is given, [i] defaults to -1.
Use array.remove(x) to remove the item by its value: remove() will remove the first occurence of item x.

This will output: array('i', [10]) array('i', [400, 600])

Reversing an Array

To reverse the items’ order in the array, we can use array.reverse():

This will output array('i', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).

Enumerating an Array

We can also enumerate the array — this will provide a clear representation of the array’s elements:

This will output:

Comparing a Python List with a Python Array

visualization of how Python array are different from Python lists

In Python, both lists and arrays serve a similar purpose — they group some elements and contain them. For this reason, various sources mix these terms up and formulate their explanations in a way that suggests that lists and arrays are the same things. We’re seeing this more in more in coding challenges — as many of these platforms are language-agnostic, they often miss the fine details of how each programming language differs from the other in its implementations of certain features (a good example being arrays). Naturally, the same misconception creeps in interview questions.

Here’s the important takeaway: Python lists and Python arrays are vastly different!
To understand arrays better, we must first analyze the lists. Which key features do they have?

  • Lists are built-in — they’re an integral part of Python as a technology and you don’t have to import them. Arrays, on the other hand, are available in the array module.
  • Lists can contain different data types. However, arrays require that the items be of the same data type.

Conclusion

Well, that’s pretty much everything you need to know about Python arrays! Let’s recap what we’ve learned:

  1. Arrays are heavily tied with memory management — when performance is critically important, they can help you out.
  2. Arrays can be modified in different ways: you can interact with their elements and add new ones.
  3. In Python, arrays and lists are not the same — you should understand the differences between them clearly in order to utilize the arrays correctly and efficiently.

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter the correct name.
Please enter the correct email.
Looks good!

Related articles

27.07.2023

Creating Our Own Chat GPT

In June, OpenAI announced that third-party applications' APIs can be passed into the GPT model, opening up a wide range of possibilities for creating ...

12.06.2023

The Ultimate Guide to Pip

Developers may quickly and easily install Python packages from the Python Package Index (PyPI) and other package indexes by using Pip. Pip ...

Building a serverless web application with Python and AWS Lambda

AWS Lambda is a serverless computing solution that enables you to run code without the need for server provisioning or management. It automatically ...

No comments yet

Sign in

Forgot password?

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account

 

By Signing In \ Signing Up, you agree to our privacy policy