Lists¶
- A list is a sequential collection of Python data values, where each value is identified by an index.
- The values that make up a list are called its elements.
- Lists are similar to strings, which are ordered collections of characters, except that the elements of a list can have any type and for any one list, the items can be of different types.
- There are several ways to create a new list. The simplest is to enclose the elements in square brackets (
[
and]
).
(chp09_01)
- The function
len
returns the length of a list (the number of items in the list). - Sublists are considered to be a single item when counting the length of the list.
(chp09_01a)
- The syntax for accessing the elements of a list is the same as the syntax for accessing the characters of a string.
- Negative index values will locate items from the right instead of from the left.
(chp09_02)
in
andnot in
are boolean operators that test membership in a sequence. We used them previously with strings and they also work here.
(chp09_4)
- Again, as with strings, the
+
operator concatenates lists. - Similarly, the
*
operator repeats the items in a list a given number of times.
(chp09_5)
- The slice operation we saw with strings also work on lists.
- Remember that the first index is the starting point for the slice and the second number is one index past the end of the slice.
(chp09_6)
Lists are Mutable¶
- Unlike strings, lists are mutable.
- This means we can change an item in a list by accessing it directly as part of the assignment statement.
- Using the indexing operator (square brackets) on the left side of an assignment, we can update one of the list items.
(ch09_7)
List Deletion¶
- Using slices to delete list elements can be awkward and therefore error-prone.
- The
del
statement removes an element from a list by using its position.
(ch09_11)
As you might expect, del
handles negative indices and causes a runtime error if the index is out of range.
Nested Lists¶
- A nested list is a list that appears as an element in another list.
- To extract an element from the nested list, we can proceed in two steps.
- First, extract the nested list, then extract the item of interest.
- It is also possible to combine those steps using bracket operators that evaluate from left to right.
(chp09_nest)
Split and Join¶
The split
method breaks a string into a list of words.
By default, any number of whitespace characters is considered a word boundary.
(ch09_split1)
An optional argument called a delimiter can be used to specify which
characters to use as word boundaries. The following example uses the string
ai
as the delimiter:
(ch09_split2)
Notice that the delimiter doesn’t appear in the result.
The inverse of the split
method is join
. You choose a
desired separator string, (often called the glue)
and join the list with the glue between each of the elements.
(ch09_join)
The list that you glue together (wds
in this example) is not modified. Also,
you can use empty glue or multi-character strings as glue.
list
Type Conversion Function¶
Python has a built-in type conversion function called
list
that tries to turn whatever you give it
into a list. For example, try the following:
(ch09_list1)
Lists and for
loops¶
It is also possible to perform list traversal using iteration by item as well as iteration by index.
(chp09_03a)
We can also use the indices to access the items in an iterative fashion.
(chp09_03b)