Lists
We encounter lists in our everyday lives. Here are some examples:
- Guest lists at parties.
- To-do lists
- Christmas wish lists.
We are going to focus on lists of numbers, but lists can store any type of value, including other lists. Here is an example:
[ 5, 7, 1 ]
This list has 3 elements, 5, 7, and 1. The list begins with a opening bracket ( [ ) and ends with a closing bracket ( ] ). The elements are separated by commas. Here are some more examples:
[ 19, 17, 13, 11, 7, 3, 1 ]
[ "Bob", "John", "Sally" ]
[ "New York", "Paris", "Sydney", "Nairobi", "Seoul" ]
The quotes around text denote strings. We can also insert variables, which is text without quotes. Here are some examples:
[ x, y, z]
[ human, alien ]
Space characters are optional. For example, all of the following lists are equivalent:
[ 5, 17, 1 ]
[5, 17, 1 ]
[5,17,1]
[5 ,17 ,1 ]
We can even spread lists across multiple lines:
[ 5, 5,
3, 7 ]
But we must have commas in between elements. So the following is invalid:
[ 5, 1 7, 1 ]
1 7 is not the same as 17. If we want a list with 4 elements, 5, 1, 7, and 1, then we need to add a comma in between 1 and 7 as follows:
[ 5, 1, 7, 1 ]
A list is a value that happens to be a collection of values. Thus, we can use lists in algebraic operations. For example, we can say:
names = [ "Bob", "John", "Sally" ]
List Index
List elements are referenced by their position. For example, suppose that list L is:
[ 12, 9, 43, 65, 382, 0 ]
- The first element of L is 12.
- The second element of L is 9.
- The third element of L is 43.
and so on.
Another word for list position is index. The index starts at 0 and increases by 1 each time we encounter a new element. Using the same list L, we can claim the following:
- The element at index 0 of L is 12.
- The element at index 1 of L is 9.
- The element at index 2 of L is 43.
and so on.
There is a shorter way to write these claims using brackets. For example the statement
The element at index 0 of L
is the same as
L[0]
Here are the same 3 claims rewritten as equations:
- L[0] = 12
- L[1] = 9
- L[2] = 43
In the following list:
[ 7, 13, 9, 20 ]
What is the value at index 3?
Replacing list elements
One of the most common list operations is element replacement (or update).
Suppose that list L is:
[ 5, 7, 1 ]
If we replace L[2] with 3, then L is now:
[ 5, 7, 3 ]
When is this useful? Consider an address book. Each element of an address book consists of a person's name and their address. If someone moves, we need to update this person's address. That is, find the person's name in the address book and replace the old address with the new one.
Consider the following list L:
[ 7, 13, 9, 20 ]
If we update L[1] to 17, then what is the new value of L?
Length of a list
The length of a list is the number of elements in the list. Consider this example:
[ 5, 7, 1 ]
This list contains 3 elements, 5, 7, and 1. Therefore, the length of this list is 3.
Here is another example:
[ "New York", "Paris", "Sydney", "Nairobi", "Seoul" ]
The length of the above list is 5.
The length of a list is useful in many situations. For example, if we have a list of homework grades for a student, then we need to know how many homework assignments there were in order to compute the student's average grade.
Consider the following list:
[ 7, 13, 9, 20 ]
What is the length of the list?
Inserting and removing elements
We can insert elements at the beginning of a list, end of a list, or in between elements. For example, consider the following list:
[ 5, 7, 1 ]
If we append 8 to the end of this list, then the list becomes:
[ 5, 7, 1, 8 ]
If we then prepend 20 to the beginning of this list, then the list becomes:
[ 20, 5, 7, 1, 8 ]
Finally, if we insert 33 in between 5 and 7, then the list becomes:
[ 20, 5, 33, 7, 1, 8 ]
Now let's remove the element at index 3. Then the list becomes:
[ 20, 5, 33, 1, 8 ]
We can keep removing elements until there are no elements left. The notation for an empty list is:
[ ]
Consider the following list:
[ 7, 13, 9, 20 ]
If we append 15 to the end of this list, the result is:
Comments
Please log in to add comments