Skip to main content

Note: Arrays have no built-in support in Python, but you can use Python Lists in their place.


Arrays

Note: Everything on this page uses LISTS as ARRAYS. Working with real arrays in Python requires importing a library, such as NumPy.

An array keeps several values together in one variable:

Example

Store three car names in an array:

Prompt
Write a Python program that creates a list called cars containing "Ford", "Volvo", and "BMW".
Output: complete Python program

What is an Array?

An array is a special kind of variable that can hold more than one value at once.

Suppose you have a list of items, such as car names. Keeping each car in its own variable might look like this:

That falls apart quickly, though. How would you loop through the cars to find a particular one? And what about 300 cars instead of 3?

An array solves both problems!

It keeps many values under one name, and you can get to any of them with an index number.

main.py
car1 = "Ford"
car2 = "Volvo"
car3 = "BMW"

Access the Elements of an Array

To reach an element of an array, use its index number.

Example

Read the first item of the array:

Prompt
Write a Python program that creates a cars list ["Ford", "Volvo", "BMW"], gets the first element, and prints it.
Output: complete Python program

Example

Give the first item of the array a new value:

Prompt
Write a Python program that creates a cars list ["Ford", "Volvo", "BMW"], changes the first element to "Toyota", and prints the list.
Output: complete Python program

The Length of an Array

The len() function returns the length of an array, meaning how many elements it has.

Note: This length is always one more than the highest index in the array.

Example

Count how many elements cars holds:

Prompt
Write a Python program that creates a cars list ["Ford", "Volvo", "BMW"] and prints its length using len().
Output: complete Python program

Looping Array Elements

A for in loop visits every element of an array in turn.

Example

Print every item in cars:

Prompt
Write a Python program that creates a cars list ["Ford", "Volvo", "BMW"] and uses a for loop to print each item.
Output: complete Python program

Adding Array Elements

To add an element to an array, call the append() method.

Example

Add one more car to the end of cars:

Prompt
Write a Python program that creates a cars list ["Ford", "Volvo", "BMW"], appends "Honda", and prints the list.
Output: complete Python program

Removing Array Elements

To take an element out of an array, call the pop() method.

You can also call the remove() method, which takes out an element by its value.

Note: If a value appears more than once, remove() only deletes its earliest match.

Example

Delete the second element of cars:

Prompt
Write a Python program that creates a cars list ["Ford", "Volvo", "BMW"], removes the second element using pop(1), and prints the list.
Output: complete Python program

Example

Delete the element whose value is "Volvo":

Prompt
Write a Python program that creates a cars list ["Ford", "Volvo", "BMW"], removes "Volvo" using remove(), and prints the list.
Output: complete Python program