Lesson 10: Arrays and Dictionaries With Python

Learn python programming in 12 lessons


Arrays are ordered arrangement of values associated with one variable. Each value in an array is identified by its unique index value starting at 0 to n-1 where n is the number of items stored in the array (n also referred to as the size).
Arrays in Python are of variable length and mixed data types can be stored in one array. Arrays can also be defined as single or multi dimensional and in Python arrays are called list’s.

a = [] —> this is an empty array
a = [1,2,3] —> array of integer with size 3
a =  ['z','y','x’] —> array of string with size 3
a = [3, 2, 1, 'z', 'y', 'x', 1] —> mixed array of size 3

Just as it was done with strings, one can retrieve a particular item from an array by accessing its index using the same slicing techniques as done on strings. From the array a = [3, 2, 1, 'z', 'y', ‘x’] the following index gives
a[0] = 3
a[4] = ‘y'
a[2:4] = [1, 'z’]
a[0:6:2] = [3, 1, 'y’]
2 in a = True
’s’ in a = False
len(a) = 6


One can also process items in a list individually by using a for loop such as printing them as opposed to printing the whole list with print(a) which gives [3, 2, 1, 'z', 'y', ‘x].

for i in a:
    print(i)

output:
3
2
1
z
y
x

for i in a:
    print(i, end=' ')

output:
3 2 1 z y x

Just like with strings, arrays do come with built in functions. To see the list of all built in functions, type help() in the shell and press return and then type list and all list functions would be displayed.

Here are some common functions and how the behave if applied on the array a

append(object) —> appends (adds) object to the end of the list
a.append(’s’) gives a = [3, 2, 1, 'z', 'y', ‘x’, ’s']

insert(object,index) —> inserts an object before the index
a.insert(4,0) gives a = [4,3, 2, 1, 'z', 'y', ‘x’]

reverse —> reverses the order of the objects
a.reverse gives a = ['x', 'y', 'z', 1, 2, 3]


Arrays can also be defined to be multi dimensional and one of the most common used is the 2-d arrays which is very common in everyday applications such as the pictures and grid games. Multi dimensional arrays in Python are created by having multiple arrays in one array such as a = [[], [], []]

We might be interested in creating a grid with 3 rows and 3 columns, then our 2-d array would look like a = [[0,0,0],[0,0,0],[0,0,0]]. Elements in a multi dimensional array are accessed with the statement a[index][index] where by the first bracket refers to the array at the index and the second bracket refers to the element at the index of the array referenced by the first bracket.
Example a = [[1,2,3], [4,5,6], [7,8,9]]
a[0] only refers to the array at index 0 which is [1,2,3]
a[0][1] refers to the element at index 1 on the array at the index 0 of array a which is element 2
a[1][2] refers to 6
a [2][1] refers to 8


You should also be aware of another Python data structure that does not care for the order of the values stored in it. This data structure is called a dictionary and it maps a set of keys to some values. Dictionaries in Python are declared such as follow

Dic = {'g': 'girl', 'b': 'boy', 'p': 'position'}

The above dictionary Dic maps the keys g,b,p to the values girl,boy,position. The following computation can be done on a dictionary.

Check if a key is in the dictionary
'g' in Dic —> True
's' in Dic —> False

Access a particular key
Dic['g’] —> ‘girl'

Get an array of the keys
Dic.keys() —> ['g', 'b', 'p’]

Get a list of the values
Dic.values() —> ['girl', 'boy', 'position’]

To see the rest of built functions that can be used to do computation on a dictionary, type help(dict) in the python shell.

Share your thoughts