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]
).
- 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.
- 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.
in
andnot in
are boolean operators that test membership in a sequence. We used them previously with strings and they also work here.
- Again, as with strings, the
+
operator concatenates lists. - Similarly, the
*
operator repeats the items in a list a given number of times.
- 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.
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.
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.
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.
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.
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:
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.
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:
Lists and for
loops¶
It is also possible to perform list traversal using iteration by item as well as iteration by index.
We can also use the indices to access the items in an iterative fashion.