8.2. Lists Are Like Strings

Besides being ordered collections, Python lists share other similarities with strings.

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

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

Try It!

Experiment with using different operators on lists. Take notes about the results you see.

  1. Does order matter? Print first_list + second_list vs. second_list + first_list, and then write down your answer.

  2. Does the + operator change the original list? To check, print first_list again after your code from step 1.

  3. Try printing first_list + 13 vs. first_list + [13]. What happens?

  4. Can we subtract two lists? Try printing first_list - second_list.

  5. What does the * operator do to a list? Try printing second_list * 3.

  6. Does the * operator change the original list? To check, print second_list again after your code from step 5.

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

Try It!

Run this program to see the results returned by the in and not in operators.

Note that even though the substring 'nana' is present in 'banana', the result of line 5 is still False. In this case, the in operator checks if the string 'nana' is its own element in fruit. To check if 'nana' is a smaller piece of each element requires more code.

8.2.4. 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]

8.2.4.1. Try It!

In the (in/not in) editor above, add slices to check only a portion of the fruit list (e.g. print("apple" in fruit[2:4])).

8.2.5. Check Your Understanding

Question

What is printed by the following code?

1
2
a_list = [4, 2, 8, 6, 5, 4]
print(a_list[3])
  1. 2
  2. 8
  3. 6
  4. 5

Question

Given num_list = [8, 6, 7, 5, 3, 0, 9], what does num_list[2:5] return?

  1. [7, 5, 3]
  2. [7, 5, 3, 0]
  3. [6, 7, 5]
  4. [6, 7, 5, 3]

Question

What is printed by the following code?

1
2
a_list = [4, 2, 8]
print(a_list * 2)
  1. [4, 4, 2, 2, 8, 8]
  2. [4, 2, 8, 4, 2, 8]
  3. [8, 4, 16]
  4. The code throws an error.

Question

Given fruit = ["apple", "orange", "banana", "cherry", "tomato", "bell pepper"], which of the following statements return True? Select ALL that apply.

  1. apples in fruit
  2. pepper in fruit
  3. banana in fruit[:3]
  4. tomato in fruit[1:4]
  5. broccoli not in fruit
  6. orange not in fruit[2:]