An array (a list) in Python is a collection of elements. Instead of just saving a value in one variable, we can use multiple lists to save multiple values at the same time.
With an array, we can store multiple values simultaneously in one variable. Let’s take a look on how we can create an array in Python. Additionally, look at some functionalities and gain a better understanding on why we use an Array.
There are a couple of things that are good to highlight – so we don’t make simple mistakes when creating an array
We create the Array by typing:
name of Array = [ value1, value2, value3, and so on.. ]
Let’s take short example and show how we create the array. If we simply write:
field = [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:
Therefore, we can extract the same values from a list in two ways, for example:
Will both give the result:
The order in which the values are assigned have a significant part for the array.
For example, the lists
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
We get the results:
That is, as you can see, two different values