Python in 5 minutes: Get started with Python arrays

Sep 17, 2021 - 5 min read
Erin Schaffer

Python is one of the most popular programming languages. It’s used in many different fields like web development, data science, machine learning, and more. Arrays are an important data structure in Python. Arrays allow us to store multiple values at a time and perform actions on those values. In this Python tutorial, we’ll explore Python arrays, array operations, and more.

We’ll cover:

Get hands-on with Python for free.

Learn the fundamentals of Python with Educative’s 1-week free trial.

Python for Programmers

What are Python arrays?

A Python array is a container that can hold a number of elements of the same data type in one single variable. This allows us to store multiple elements of the same type together. Arrays are very common. Most data structures use arrays to execute algorithms, and arrays are very commonly used in fields like data science and machine learning. We can use arrays when we want to manipulate data of a certain type. They’re very useful when working with large amounts of data because arrays can hold a large number of elements.

An array element is an item stored in an array. So the items “Spot”, “Max”, and “Sam” from our dogs array are elements. The index refers to the numerical index location of each element. We can use the index to identify and access elements.

What are Python lists?

Although the two are sometimes confused, a Python list is different than a Python array. A list is a collection of items that contains data of different data types. This means that the first element of the list can be a string, the second element can be an integer, and the third can be a list of strings, etc. Lists are ordered, mutable, and their elements don’t need to be unique.

We can convert our Python lists to arrays using the NumPy Python library using one of the following NumPy array functions:

  • numpy.array()
  • numpy.asarray()

Create Python array

We can create a new array using the array module and the import array command. Let’s take a look at an example Python program:

dogs = ["Spot", "Max", "Sam", "Charlie", "Cooper", "Duke", "Bear", "Buddy", "Milo", "Murphy"]


Now, let’s take a look at the different operations we can perform on Python arrays.

Common array operations

Add elements

There are a couple of different ways to add elements to an array. We can append elements to the end of the array using the append() method:

dogs = ["Spot", "Max", "Sam", "Charlie", "Cooper", "Duke", "Bear", "Buddy", "Milo", "Murphy"]



We can use the insert() method to add an element to a given index location within our array. Here’s an example:

dogs = ["Spot", "Max", "Sam", "Charlie", "Cooper", "Duke", "Bear", "Buddy", "Milo", "Murphy"]

dogs.insert(1, "Wrigley")


Access elements

We can access an array item by referring to its index number. For example, if we want to get the value of the first array item, we can use the following code:

dogs = ["Spot", "Max", "Sam", "Charlie", "Cooper", "Duke", "Bear", "Buddy", "Milo", "Murphy"]

x = dogs[0]


If we want to access a specific element and change that element, we access the index number and set it equal to the modified value. For example:

dogs = ["Spot", "Max", "Sam", "Charlie", "Cooper", "Duke", "Bear", "Buddy", "Milo", "Murphy"]

dogs[0] = "Jack"


Remove elements

There are a couple of different ways to remove elements of an array. We can use the pop() Python function to remove an element at a specified position. Let’s say we want to remove the last element of our dogs array:

dogs = ["Spot", "Max", "Sam", "Charlie", "Cooper", "Duke", "Bear", "Buddy", "Milo", "Murphy"]



We can use the remove() method to remove a specified element from an array. Here’s an example:

dogs = ["Spot", "Max", "Sam", "Charlie", "Cooper", "Duke", "Bear", "Buddy", "Milo", "Murphy"]



Find array length

We can use the len() method to return the length of an array. If we want to return the number of elements in the dogs array, we can use the following code:

dogs = ["Spot", "Max", "Sam", "Charlie", "Cooper", "Duke", "Bear", "Buddy", "Milo", "Murphy"]

x = len(dogs)


Keep learning Python for free.

Get started with Python fundamentals for free with our 1-week Educative Unlimited Trial. Educative’s text-based learning paths are easy to skim and feature live coding environments, making learning quick and efficient.

Python for Programmers

Sort Python array

We can use the sort() method to sort our array in ascending or descending order. If we want to sort our array in ascending order, we can use the following code:

numbers = [5, 13, 25, 2, 98, 56, 4, 8]



To sort our array in descending order, we can use the following code:

numbers = [5, 13, 25, 2, 98, 56, 4, 8]



Count elements

We can use the count() method to return the number of elements with the specified value. For example, let’s say we want to return the number of times the value “Spot” appears in our dogs array:

dogs = ["Spot", "Max", "Sam", "Charlie", "Cooper", "Duke", "Bear", "Buddy", "Milo", "Murphy"]

x = dogs.count("Spot")


2D Python arrays

A 2D array, or a two-dimensional array, is when there’s an array within an array. In a standard array, each element has an index. In a 2D array, each element has two indices. Let’s move on to an example. Imagine we need to keep track of how the temperature changes throughout the day. We’ll take four temperatures: one in the early morning, one in the late morning, one in the afternoon, and one in the evening. We can store these temperatures in a 2D array.

Day one

Day two

Day three

Day four

















temperatures = [[52, 60, 66, 63], [50, 58, 62, 60], [53, 61, 67,64], [51, 59, 65, 62]]

We can perform the same actions on 2D arrays as we can on standard arrays.

Wrapping up and next steps

Congrats on taking your first steps with Python arrays! Arrays are an important and common data structure. They have a wide range of uses and are commonly used to execute algorithms. There’s still a lot more to learn about the Python programming language. Some recommended concepts to cover next include:

  • Python dictionaries
  • Python tuples
  • Python strings
  • Python syntax

To get started learning these concepts and more, check out Educative’s learning path Python for Programmers. In this path, you’ll start by learning the fundamentals of Python, and then move into more advanced concepts including modules and web-related tasks. By the end, you’ll have the advanced knowledge to confidently use Python in your next project.

Happy learning!

Continue learning about Python

WRITTEN BYErin Schaffer

Join a community of 500,000 monthly readers. A free, bi-monthly email with a roundup of Educative's top articles and coding tips.