List Basics
The Python list data type is another example of an ordered collection. Lists store data values, which are called elements. Just like with strings, each element has its own index value. However, strings are ordered collections of characters and the elements of lists can be of any data type.
Create a New List
There are several ways to create a new list. The simplest is to enclose the
elements in square brackets []
, with each element separated from the others
by commas.
|
|
- Line 1 assigns a list of five integers to the variable
numbers
. - Line 2 assigns a list of four strings.
- The elements of a list don’t have to be the same data type! The list in line 3 contains a string, an integer, a boolean, a float, and another list.
- Line 4 assigns a special list that contains no elements, called the empty list.
A list within another list is said to be nested. We will explore this idea later in the chapter.
Accessing Elements
With strings, we accessed individual characters by using square brackets. With lists, we use square brackets to access list elements. The integer or expression inside the brackets gives the index for the element we want.
Any integer (or an expression that returns a whole number) can be used as the
index. Negative index values identify elements from right-to-left, beginning
at -1
for the last element in the list.
Besides being ordered collections, Python lists share other similarities with strings.
List Length
The len()
function also returns the length of a list (the number of elements in the list).
|
|
Console Output
The list ['a', 'b', 'c', 'x', 'y', 'z'] has 6 elements.
In line 3, len(letters)
returns the number of items stored in the
letters
list.
Note that the statement print(letters[len(letters)])
will throw an index
out of range error. Since index values start at 0, the last element in any
list will always have a value of len(list_name) - 1
.
Combining Lists
Just like strings, we can use the +
and *
operators for concatenation
and repetition. Concatenation combines different lists to create one new,
longer list. Repetition makes multiple copies of the same elements within a
single list.
|
|
Console Output
[1, 2, 3, 4, 5, 6]
[4, 5, 6, 1, 2, 3]
[1, 2, 3, 1, 2, 3, 1, 2, 3]
in
and not in
Just like strings, we can use the in
and not in
operators to check if a
specific value is present in a list. The operators return True
or False
depending on if the value matches an element.
first_list = [1, 2, 3]
print(10 in first_list)
Console Output
False
List Slices
Just like strings, we can return a slice (several elements) from a list. Taking a slice creates a new list, and the syntax should be familiar:
list_name[start_index : end_index]
The new list contains the elements from start_index
up to but NOT including
end_index
. If we leave out start_index
, the slice starts at the
beginning of the list. If we leave out end_index
, the slice continues to
the end of the list.
The index values in the new list begin at 0.
|
|
Console Output
[6, 8, 10] vs. [2, 4, 6, 8, 10, 12, 14]
6
[2, 4, 6]
[8, 10, 12, 14]
Check Your Understanding
Identify the length of these two lists. (The answers list classes
first,
then teachers
).
classes = ["Chemistry, US History, Intro To Coding"]
teachers = ["Cortez", "Holmes", "Bracey"]
- 1 and 3
- 3 and 1
- 3 and 3
- 1 and 1
Identify the output from the following statements:
a_list = ["Hello", 42, True, 3.14]
print(a_list[2])
Hello
42
True
3.14