# Lists

An application often needs to store a process quantity of data.

For example:

• Daily temperatures - to find highs, lows, or averages
• A list of names (strings) - to lookup a name
• A list of URLs - browser history

Python uses a type called a list to store a number of data items.

A Python list is called an array in most other programming languages.

We will use the terms list and array interchangeably.

## Individual Variables

Recall, a variable is a named storage location.

count = 31
size = 8.47
greet = 'Hello'

Diagram of the above variables.

+---------+
count       |      31     |
+---------+
size          |    8.47   |
+---------+
greet        | 'Hello'  |
+---------+

Each variable above has a single value.

## Lists/Arrays

A list is an ordered group of variables with the same name.

Subscripts, also called an index, denote individual items of a list.

A list named arr that has the values 21, 6, 9, 2.

arr = [21, 6, 9, 2]

In programming languages the first subscript is 0, not 1.

### List Subscript Notation

Subscripts are usually written inside square brackets, [ ].

arr[1] = 6

arr[3] = 2

## Python Lists

Python code to create the above list:

arr = [21, 6, 9, 2]
# Output the 9
index = 2
print(arr[index])

The subscript for the 9 is 2.

What would the Python statement be to output the 21 in the above list?

## List Size and Valid Subscripts

Using the above list execute the following code:

# Set the value of the index variable to 4
index = 4
# Output the value of at the index
print(arr[index])

Notice the value of the greatest valid subscript is one less than the size.

This is because the first subscript is 0.

• If an array has 100 elements the valid subscripts are 0 through 99.
• If an array has 55 elements the valid subscripts are 0 through 54.
• If an array has N elements the valid subscripts are 0 through N - 1.

## Outputting the Elements in a List

Define a list and output the values in the list:

# A list to work with
data = [3, 1, 7, 4]
# Output the list elements
index = 0
while index < 4:
print(data[index])
index = index + 1

## List Size and Valid Subscripts - Common Error

A common error:

# A list to work with
data = [3, 1, 7, 4]
# Output the list elements
index = 0
while index <= 4:
print(data[index])
idx = index + 1

What is the problem?

## Using Lists

items = [] # Create an empty list named items
print(items[0]) # Error, invalid subscript
items = [3, 5, 8] # Create a list with 3 elements
print(items[0]) # OK
print(items[3]) # Error, invalid subscript

A list must have an element for a subscript before the subscripting is done.

## Lists, the len Function

Given a list it would be convenient to know how many elements are in it.

The len function:

Function Name: len
Argument: A single argument of type list.
Result: An integer is returned that is the number of elements in the argument list.
Side Effect: None.

Example:

arr = [ 3, 4 ]
count = len(arr)

## Putting Items in an Empty List

items = [] # Create an empty list
items.append(3) # Append 3
items.append(5) # Append 5
items.append(8) # Append 8
print(items[0])  # OK

This is very convenient. Start with an empty list and put in as many items as we want.

If we specified the list size to be 10 (and were able to do that), then we would have to store 10 items in each of the 10 subscripts or keep track of how many valid items were in the list if we used less than 10 subscripts.

## General Outputting of a List

If a list is constructed using append, the number of elements in the list can vary.

The len function must be used in outputting.

The following code will output the indices in order with each index followed by the element in the list at that index.

# Assume list arr has some items in it

index = 0
while index < len(arr):
print(index, arr[index])
index = index + 1

## while loop break statement

A while loop repeats a group of statements as long as the while condition is true.

Sometimes it is convenient to stop the repetition in the middle of the loop statements.

A break may be used to terminate while loop repetition.

When the break executes:

• No further statements in the loop are executed.
• The statement that follows the loop is executed next.

while CONDITION:
# ...
if ANOTHER_CONDITION:
break
# ...

print("After the loop")

If ANOTHER_CONDITION is true the break will execute and the repetition will be terminated.

## Inputting and Outputting of a List

Write a program that will input numbers from the user, store the numbers in a list, compute the sum, and output the sum.

A list is used so that other operations on the data, such as find the greatest item, may be done.

Assume the input is terminated by a -1.

# Input
data = []
while True:
num = input('Please enter a number, -1 to quit: ')
if int(num) == -1:
break
data.append(int(num))
# Compute
sum = 0
idx = 0
while idx < len(data):
sum = sum + data[idx]
idx = idx + 1
# Output
print('Sum is', sum)