Array in Python


Keywords: array, list, variable

Array Python Programming

An array in Python is a collection of elements where each element is being assigned an index number and are consisting of the same data type. The elements can be selected and used by using the index connected to each element.

Array in Python

An array in Python is used to store multiple values in a single variable, instead of declaring separate variables for each value. In other words, a array is a collection of elements stored in what can be described as a list. Each element in an array can be retrieved and used with its index.

For example, what if we have a hundred values that we want to save at the same time? A thousand values? One million values? In order to handle a very large amount of data at the same time, we use arrays (also called lists and sometimes fields). Array in Python allows us to save multiple values in one variable. That is, instead of creating separate variables for each value, we can instead create an array that contains all values at the same time.

In Python, an array is a collection of elements, where each element is numbered and of the same data type.

When we develop our programs we will often handle collections of objects of the same type and then an array is a very useful tool to be able to structure and group the objects together.

What is an array in Python?

We have previously seen how we can store values in variables, for example:

var = 10

The variable var, of type integer, is assigned the value 10. We can illustrate it as:

Array Variable Python Programming

Similarly, we can now think of an array as a box with several boxes inside.

Array Python Programming

Furthermore, we will see how we can access each box using its index, that is, the element’s position in an array. Therefore, we can use the value, read it, and even edit it. It is important to note that indexing always starts at 0; that is, the first value gets index 0, the second value gets index 1, and so on.

Create Array in Python

  • We start by specifying the array’s name, followed by a equal sign =
  • After the equal sign we put square brackets [ ]
  • Finally, we enter the data type that we have chosen to work with followed by the number of elements we want to create in square brackets [ ]

Syntax: Declare array in Python

If we use the bullets in the list above

# Syntax Array
name of Array = [ value1, value2, value3, and so on.. ]

Example: Create array in Python

Let’s create a small array

  • We name it arrEx
  • arrEx will contain four different values
  • We can then access each value by using the index
# Example array named arrEx
arrEx = [22, 8, 97, 3]
Array Example Python Programming

Index for array in Python

The indexes (location) of each element are arranged from 0, 1, 2, 3… in ascending order.

Thus, the first value in the list has index 0. Second value index 1 and so on. There is also a corresponding negative indexing of arrays. This means that the last value also has index -1, the second last value -2 and so on. Each value has two indexes, as shown in the figure below:

Example Array Python Programming

So if we use the array named arrEx the we previously created and write

# print values from the array arrEx
print(arrEx[-1])
print(arrEx[3])

We will get the result

3
3

The elements in the Array have a specific order

The order in which the values are assigned have a significant part for the array. For example, the array

# Example of list in python
my_list = [5, 2, 8, 9, 3]
my_list2 = [2, 5, 9, 8, 3]

exactly the same values. But since the values are not in the same order, the results will be different. Say that we want to use the first element from each of those Arrays

print(my_list[0])
print(my_list2[0])

We get the results:

5
2

Lists in Python and data types

The values in an Array do not have to be of the same data type, therefore, you can mix different data types in the same list. For example, you can have int, strings and lists in the same list! This is a major difference compared to other major programming languages, such as Java, where you need to keep track of the data type an Array contains since you cannot mix data types.

But now we are focusing on Array in Python so let’s take a look at how we can create a Array that contains an Integer, Text String and another Array inside it.

# Example of storing different data types within the same list
my_list2 = [2, "Python", "Python", 23, [3, 5, 10]]

And if we decide to print the results

# print the result for my_list2
print(my_list2)

we get:

[2, 'Python', 'Python', 23, [3, 5, 10]]

Useful features for an Array in Python

Arrays in Python have several built-in features which makes it very easy to work with. Below are some of the most common features of arrays.

Suppose we have the same array, called my_list,  in the following example

# create a example list to use
my_list = [5, 2, 8, 9, 3]

Size of the Array in Python

The size of a Array can be retrieved by using the built-in function, len() 

# the size of the array my_list
len(my_list)
Size of Array Python Programming

Results in:

5

Since the Array contains 5 elements.

Print multiple variables within a given range

By simply typing

print(my_list[2:4])

we will get the result

[8,9]

Note, the value at index 4 is not printed. In other word, typing for example print(2:4) will print up to 4, but not include index 4.

Change the value of an element

Okay, let’s say that we want to modify the value in our Array, we simply write:

my_list[1] = 999

And if we then print the result we see that

[5, 999, 8, 9, 3]

the value at index 1 has changed from 2 to 999.

Change value Array Python Programming

Add new values to the array in Python

By using the built-in function append() you can easily add new values to your Array. 

Let’s add the value 13 to our Array

# Add values to array my_list
my_list.append(13)
print(my_list)

Results in

[5, 999, 8, 9, 3, 13]

Note, that the value is added at the end of the Array

Summary: Array in Python

An array in Python is used to store multiple values in a single variable, instead of declaring separate variables for each value. Therefore, an array is a collection of fixed elements in what can be seen as a list. Each element in an array can be retrieved and used by using the connected index.

Syntax: Create array in Python

# Syntax Array
name of Array = [ value1, value2, value3, and so on.. ]