Keywords: array, list, variable
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.
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.
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:
Similarly, we can now think of an array as a box with several boxes inside.
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.
If we use the bullets in the list above
# Syntax Array name of Array = [ value1, value2, value3, and so on.. ]
Let’s create a small array
# Example array named arrEx arrEx = [22, 8, 97, 3]
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:
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 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
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]]
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]
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)
Results in:
5
Since the Array contains 5 elements.
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.
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.
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
Please leave feedback and help us continue to make our site better.
How useful was this article?
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?
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 Array name of Array = [ value1, value2, value3, and so on.. ]