Order.. matters.. And in Python, lists make it easy to work with ordered data. Lists are
a built-in data structure for storing and accessing objects which belong in a specific
sequence. We will now learn how to create and use lists, and we will do so in a linear
and orderly fashion...
There are two ways to create a list. One way
is to use the list constructor. But a simpler and more common way is to use brackets.
When creating a list, you can also pre-populate it with values. For example, let’s create
a list with the first few prime numbers.
You can always add values later by using the “append” method which allows you to add
new values to the end of the list. Let’s append the next two prime numbers: 17… and
If you display the list, you will see it contains the new values. Notice how lists preserve
the order of the data - this is different from sets. In sets, the order is not important.
In lists, order is everything. You do not have to view the entire list. If
you want to see a specific value, you can access it by its index. In computer science,
you start counting with 0, not 1. So the elements in our list “primes” are indexed 0.. 1..
2.. 3.. 4.. 5.. 6.. and 7.. To view the first item, you type the name
of the list, and the index in brackets. The first item is 2.
The second item has index 1, and the second item is 3.
And so on…
Notice how the indices increase by one as you go from left to right.
And they decrease by one when you go from right to left.
When you get to the beginning, the index is 0. If you decrease the index once more, you
get -1. Here, Python wraps back around to the end of the list. So the last item is -1,
the next to last is -2, and so on. This is convenient when you want to look at
the values at the end of a list. The last item is 19…
The next to last prime is 17…
And we reach the beginning of the list with index -8.
Be careful - you can only wrap around once. If you try to find the value of index -9,
you get an index error.
Another way to access values in a list is
by slicing. This lets you retrieve a range of values from your list.
We will continue to use our list of primes.
To slice this list, type the name of the list, bracket, a starting index, a colon, a stopping
index, then a closing bracket… The result is a sublist that starts at index 2, and continues
until it reaches index 5. Be careful. Slicing includes the value at the starting index,
but excludes the stopping index. The beginning value is included; the ending value is not.
One more slice… This will start at the beginning, which is index 0, and continue to index 6,
which is 17. It will not include the final number, so this slice includes the primes
from 2 through 13.
Lists can contain more than prime numbers.
They can contain integers…booleans… strings… floats… and even other lists.
Many languages require lists to contain values of the same type, but not Python. With Python,
you are free to insert multiple data types in the same list.
Lists can also contain duplicate values. Here is another way lists are different from sets.
For example, suppose you want to record the numbers you roll on a pair of dice. Pretend
you roll a 4.. 7.. 2.. 7.. 12.. 4 and 7.. If you look at the list, all the values are
there, even the repeated rolls.
You can also combine lists. To see how, create
two separate lists: A list of numbers…
And a list of letters… To combine these two lists into a single list,
use the plus sign. Numbers + letters equals 1, 2, 3 .. a, b, c...
But order matters. If you reverse this and
compute letters + numbers you get a, b, c.. 1, 2, 3..
Combining lists is called “concatenation”.
Observe. The list of numbers..
and the list of letters are unchanged.
There are many other methods for working with
lists. To see them all, pass any list to the directory function.
To learn how to use one of these methods, use the help function. For example, there
is a method for reversing a list. The help text gives full details on what it does and
how to use it.
Lists start at 0 and end… ... they end precisely
when you are finished. You can slice them… You can concatenate them… You can reverse
them… You can even clear them… If I were to make a list of all the uses of lists, I
would have a very, VERY long list…