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.

A label, languages, pointing to an array that contains “Python” at index 0, “C#” at index 1, “Java” at index 2, and “JavaScript” at index 3. A label, languages, pointing to an array that contains “Python” at index 0, “C#” at index 1, “Java” at index 2, and “JavaScript” at index 3.

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.

Example
1
2
3
4
numbers = [5, 15, 15, 0, 25]
strings = ['banana', 'broccoli', 'kale', 'applesauce']
mixed_data = ["Hello", 42, True, 3.14, [-3, 48.5]]
empty_list = []
  1. Line 1 assigns a list of five integers to the variable numbers.
  2. Line 2 assigns a list of four strings.
  3. 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.
  4. Line 4 assigns a special list that contains no elements, called the empty list.
Note

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).

Example
1
2
3
letters = ['a', 'b', 'c', 'x', 'y', 'z']

print("The list {0} has {1} elements.".format(letters, len(letters)))

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.

Example
1
2
3
4
5
6
first_list = [1, 2, 3]
second_list = [4, 5, 6]

print(first_list + second_list)
print(second_list + first_list)
print(first_list * 3)

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.

Example
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.

Example
1
2
3
4
5
6
7
8
original_list = [2, 4, 6, 8, 10, 12, 14]

new_list = original_list[2:5]

print(new_list, 'vs.', original_list)
print(new_list[0])
print(original_list[:3])
print(original_list[3:])

Console Output

[6, 8, 10] vs. [2, 4, 6, 8, 10, 12, 14]
6
[2, 4, 6]
[8, 10, 12, 14]

Check Your Understanding

Question

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. 1 and 3
  2. 3 and 1
  3. 3 and 3
  4. 1 and 1
Question

Identify the output from the following statements:

a_list = ["Hello", 42, True, 3.14]
print(a_list[2])
  1. Hello
  2. 42
  3. True
  4. 3.14