Working with Arrays

Introduction

Sometimes we need to store a list of values, or a sequence of data. In a situation like this, we use an array. As explained in the section on Data Types, an array is a composite data type, which means it is made up of a number of primitive data types. In this section we will learn what arrays are, and how to use them.


What is an Array?

An array is a collection of variables, each identified by an index number. One way of visualizing an array is thinking about a row of containers, each numbered, starting with 0, 1, 2, 3, and so on. Then, when you assign a variable to position 0, it is like placing that object into the container marked 0. Then you add another variable by placing it into the next container labelled 1, and so on.


Creating an Array

Let's start with a simple example of creating an array of Fruit. In this example we will have three String variables contained in the array. In the first (index 0) "container" we will put "Apple", in the second (index 1) we will put "Banana", and in the third (index 2) we will put "Cherry".

Open up the Python interactive interpreter and try out the following code:

my_array = ["Apple", "Banana", "Cherry"]
print my_array
[Output: ['Apple', 'Banana', 'Cherry']]
print my_array[0]
[Output: Apple]
print my_array[1]
[Output: Banana]
print my_array[2]
[Output: Cherry]

Inside memory, array elements are stored in sequential addresses. To investigate this, let's use the id() function to get the memory addresses:

my_array = ["Apple", "Banana", "Cherry"]
id(my_array)
[Output: 4299672896]
id(my_array[0])
[Output: 4300015440]
id(my_array[1])
[Output: 4300015680]
id(my_array[2])
[Output: 4300015728]

Note: The specific values for the memory addresses will be different, but it gives you an idea of the memory allocations for array elements.


Array Operations

Let's now look at manipulating the array.

Adding items

To add an item onto the array, we use the .append() function.

my_array = ["Apple", "Banana", "Cherry"]
my_array.append("Date")
print my_array
[Output: ['Apple', 'Banana', 'Cherry', 'Date']]

Removing Items

To remove an item from the array, we use the .remove() function.

my_array = ["Apple", "Banana", "Cherry", "Date"]
my_array.remove("Date")
print my_array
[Output: ['Apple', 'Banana', 'Cherry']]

Reverse Order of Array

To reverse the order of an array, we use the .reverse() function.

my_array = ["Apple, "Banana", "Cherry"]
my_array.reverse()
print my_array
[Output: ['Cherry', 'Banana', 'Apple']]

Sort Array

To sort an array alphabetically, we use the .sort() function.

my_array = ["Grape", "Fig", "Pineapple", "Banana"]
my_array.sort()
print my_array
[Output: ['Banana', 'Fig', 'Grape', 'Pineapple']]

Retrieve Information about the Array

Number of Items in the Array*

my_array = ["Apple", "Banana", "Cherry"]
num_items = len(my_array)
print num_items
[Output: 3]

Minimum and Maximum Values

my_array = [100, 43, 7000, 12]
min_value = min(my_array)
print min_value
[Output: 12]

max_value = max(my_array)
print max_value
[Output: 7000]

Exercises

  1. Create an array of five animal names, sort the list and then reverse the array.
  2. Create an array of numbers from 10 to 15 and find the memory addresses for each of the six elements in the array.
  3. Create two arrays, one with three fruits, and one with three vegetables. Create a new variable by adding the two arrays together (arr_fruit + arr_veg) and print out the value of the new variable. What do you notice happens when you add two arrays together?

Continue to next section


Comments

comments powered by Disqus