Lists

Post Office Boxes Post Office Boxes ^[File:USPS Post office boxes 1.jpg. (2017, May 17). Wikimedia Commons, the free media repository. Retrieved 18:17, November 5, 2018 from https://commons.wikimedia.org/w/index.php?title=File:USPS_Post_office_boxes_1.jpg&oldid=244476438.]

Arrays allow us to store multiple values in the same variable, using an index to determine which value we wish to store or retrieve from the array. We can think of arrays like a set of post office boxes. Each one has the same physical address, the post office, but within the post office we can find an individual box based on its own box number.

Some programming languages, such as Java, use arrays that are statically sized when they are first created, and those arrays cannot be resized later. In addition, many languages that require variables to be declared with a type only allow a single variable type to be stored in an array.

Other languages, such as Python, use lists in place of arrays. List can be resized, and in untyped languages such as Python they can store different data types within the same list.

Arrays in Flowcharts & Pseudocode

The table below lists the flowchart blocks used to represent arrays, as well as the corresponding pseudocode:

Operation Flowchart Pseudocode
Declare Array Declare Array Flowchart Block Declare Array Flowchart Block
ARR = new array[5]
Store Item Store Item in Array Flowchart Block Store Item in Array Flowchart Block
ARR[0] = 5 
Retrieve Item Retrieve Item from Array Flowchart Block Retrieve Item from Array Flowchart Block
X = ARR[0]

Lists in Python

Let’s review the syntax for working with lists in Python.

List Creation

To define a list in Python, we can simply place values inside of a set of square brackets [], separated by commas ,:

We can also create an empty list by simply omitting any items inside the square brackets

Adding Items

Once we’ve created a list in Python, we can add items to the end of the list using the append() method:

Accessing List Elements

Once the list is created, we can access individual items in the list by placing the index in square brackets [] after the list’s variable name:

Multidimensional List

Python lists can also be created with multiple dimensions, simply by appending lists as elements in a base list.

They can also be created through the use of lists as individual elements in a list when it is defined:

To access elements in a multidimensional list, simply include additional sets of square brackets containing an index [] for each dimenison:

List Operations

There are several operations that can be performed on lists in Python as well:

List Loops

Finally, we can use a special form of loop, called a For Each loop, to iterate through items in a list in Python:

Once important thing to note is that lists accessed within a For Each loop are read only. So, we cannot change the values stored in the list using this loop, but we can access them. If we want to change them, we should use a standard For loop to iterate through the indices of the list:

References