What Is an Array in Python: A Beginner's Guide to Python Arrays

Danielle Ellis
Danielle Ellis

Updated:

Published:

Picture an array as a giant container that can hold any type of item. It could be a box divided into small square compartments or a large tub with different areas designated for specific items. In either case, the array can store many elements at once, making it easy to grab whatever you need.

Software developer learning about Python arrays

Download Now: An Introduction to Python [Free Guide]

Python arrays work similarly- they are containers that can hold any type of data, including numbers, strings, and even other arrays. This makes them powerful programming tools because you can create complex data structures without worrying about running out of space.

This post will cover what arrays are, how to create them, and some of the most common operations you can perform on them. By the end, you should understand how to use python arrays in your own code.

What is an Array in Python

An array is a data structure that stores values of the same type. In python, this is the main difference between arrays and lists. All values in an array must be of the same type, but they can be different types in a list.

For example, you could have an array that contains only integers or one that contains only strings, but you could not have an array that contains both integers and strings. However, you could have a mix of different data types in a list.

Contrary to working with lists, when you access an array item, python has to do much less work to find it. In addition, arrays can be used in many different ways that lists cannot, such as for matrices and multidimensional data.

Now that you know what an array is, let's look at how to create one in python.

How to Make an Array in Python

In python, there are several ways to create an array. The most common is to use the built-in array module. This module provides functions for creating arrays, including ones with a specific data type.

To use the array module, you first need to import it. Once you have imported the array module, you can create an array by calling the array() function. This function takes two arguments: the data type of the values in the array and an optional list of values.

For example, to create an array of integers, you would use the following code:

import array int_array = array.array('i', [-1, 0, 1, 2])

This code creates an array of integers (-1, 0, 1, 2) with the data type 'i'. The data type 'i' is used for signed integers. There are other data types that can be used for other types of values.

You can also create an empty array by omitting the second argument:

empty_array = array.array('i')

This creates an empty array that can store integers. You can then add values to it later.

Python Array Operations

Now that we know how to create arrays let's look at some of the most common operations you can perform on them.

Indexing

Indexing is used to access specific values in an array. You can index an array using square brackets ([]) after the array name. The number inside the brackets is called the index.

Example:

Given the following array, you could get the value at index 2 like this:

int_array = array.array('i', [-1, 0, 1, 2]) print(int_array[2]) // 1

The above example would print 1 since that is the value at index 2.

Note: Python begins counting at 0, not 1. So the first item in the array would be at index 0, the second item would be at index 1, and so on.

Adding Elements to a Python Array

You can add items to an array using the append() or extend() methods. The append() method adds a single item to the end of an array, while extend() can be used to add multiple items to the end of an array.

Example:

Given the following array, you could add the value 2 to the end like this:

int_array = array.array('i', [-1, 0, 1]) int_array.append(2) // [-1, 0, 1, 2]

You could also add the values [-2, -1, 0] like this:

int_array.extend([-2, -1, 0])

This example would result in the following array:

int_array = array.array('i', [-1, 0, 1, 2, -2, -1, 0])

Changing Elements of a Python Array

If you want to insert an item at a specific index, you can use the insert() method. This method takes two arguments: the index to insert at and the value to insert.

Example:

Given the following array, you could insert the value 2 at index 1 like this:

int_array = array.array('i', [-1, 0, 1]) int_array.insert(1, 2) // [-1, 2, 0, 1]

Removing Elements from an Array

If you want to remove an item from an array, you can use the remove() method. This method takes a single argument: the value to remove.

Example:

Given the following array, you could remove the value -1 like this:

int_array = array.array('i', [-1, 0, 1]) int_array.remove(-1) // [-1, 0]

You can also remove an item at a specific index by using the pop() method. This method takes a single argument: the index to remove.

Example:

Given the following array, you could remove the item at index 1 like this:

int_array = array.array('i', [-1, 0, 1]) int_array.pop(1) // [-1, 1]

You can also remove the last item in an array using the pop() method without an argument.

Example:

Given the following array, you could remove the last item like this:

int_array = array.array('i', [-1, 0, 1]) int_array.pop() // [-1, 0]

Iterating through an Array

You can iterate through an array using a for loop.

Example:

Given the following array, you could print each value like this:

int_array = array.array('i', [-1, 0, 1]) for i in int_array:    print(i)

The example would print -1, 0, and 1 on separate lines.

Sorting a Python Array

If you want to sort an array, you can use the sort() method. This method sorts the values of an array in place.

Example:

Given the following array, you could sort it like this:

int_array = array.array('i', [-1, 0, 1]) int_array.sort() // [-1, 0, 1]

The example would result in the following array:

int_array = array.array('i', [-1, 0, 1])

This is a list of the most common operations you can perform on Python arrays. There are many more operations you can perform, but these would be a great start to learn about how to manipulate data in Python.

Final Thoughts on Using Python Arrays

As discussed, an array is a collection of data put into order. This can be helpful when using python because it helps to keep your code clean and organized. Arrays are similar to lists but are more efficient when doing math operations. Python arrays are usually used for scientific computing purposes. You should now have a better understanding of what an array is and how to use one in python. To further your learning,  you can check out the NumPy library, which is built on top of arrays

python

Topics: What Is Python?

Related Articles

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

A guide for marketers, developers, and data analysts.