This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 3: Object methods

List methods

face Josiah Wang Oana Cocarascu

Hope those exercises were challenging enough! 😀

Now, let’s explore the methods for Python’s list object - again, with a quiz!

Again, you might want to have the official documentation for list at hand. The answers you seek will all be there!

Remember that many of these list methods are mutator methods that directly change the value of the list, rather than returning a new list!

1 2 3 4 5 6 7 8 9 10

Question 1

Given numbers = [2, 3], write a method call to add the integer 5 to the beginning of numbers, i.e. [5, 2, 3].

numbers.insert(0, 5)
Explanation:

The method list.insert(position, element) inserts an element to a list at a specific position (index). So you can add an element to anywhere in your list.

Question 2

Given numbers = [2, 5, 3, 5, 1, 3] and query = 5, write a method call that automatically returns the index of the first occurrence of query in numbers. The expected return value for this example is 1, since the first occurrence of 5 is at index 1.

numbers.index(query)
Explanation:

Use list.index(query) to search for the first occurrence of query in a list. There are also two optional parameters that limits the search to specified start/end indices (see documentation for more details).

Question 3

Given numbers = [2, 5, 3, 5, 1, 3] and query = 5, write a method call that automatically removes the first occurrence of query in numbers. After this method call, numbers should be [2, 3, 5, 1, 3], since the first occurrence of 5 has been removed.

numbers.remove(query)
Explanation:

Use list.remove(query) to remove the first occurrence of query in a list. This method will result in an error if the query does not exist in the list.

Question 4

Given numbers = [2, 5, 3, 5, 1, 3] and query = 5, write a method call that automatically counts the number of times query occurs in numbers. The expected return value for this example is 2, since 5 occurs twice in numbers.

numbers.count(query)
Explanation:

Similar to the .count() method in str, list.count() counts the number of times an element appears in the list.

Question 5

What is the output of the following piece of code?

1
2
3
letters = ["c", "a", "b"]
letters.sort()
print(letters)
["a", "b", "c"]
Explanation:

Note that the list.sort() method sorts elements in the list in place. This means it moves elements around in the list directly and does not return a copy of the list (it returns None). Use the built-in function sorted(list) if you need to return a copy of the sorted list, rather than modifying the existing list.

Question 6

What is the output of the following piece of code?

1
2
3
letters = ["c", "a", "b"]
letters.reverse()
print(letters)
["b", "a", "c"]
Explanation:

Like list.sort(), list.reverse() reverses the elements in the list in place and does not return a copy of the list.

If you wish to return a copy of a reversed list instead, use the list slicing idiom (i.e. numbers[::-1]).

For something more readable, there is also a built-in function reversed(your_list) if you do not wish to modify the existing list, but just need a copy of the reversed list returned. Note, however, that reversed() returns an iterator which can be used for example as part of a for loop (e.g. for number in reversed(numbers)). You will have to explicitly convert it to a list if you need a list, i.e. list(reversed(numbers)).

Question 7

What is the output of the following piece of code?

1
2
3
letters = ["c", "a", "b"]
letters.clear()
print(letters)
[]
Explanation:

The list.clear() method removes all elements in a list.

Question 8

What is the output of the following piece of code?

1
2
3
4
numbers = [3, 4, 5]
additional_numbers = [1, 2]
numbers.append(additional_numbers)
print(numbers)
[3, 4, 5, [1, 2]]
Explanation:

The .append() method adds a new element to the list. Remember that .append() is designed to add ONE item to a list (the one item can be anything, including a list!)

Question 9

What is the output of the following piece of code?

1
2
3
4
numbers = [3, 4, 5]
additional_numbers = [1, 2]
numbers.extend(additional_numbers)
print(numbers)
[3, 4, 5, 1, 2]
Explanation:

The numbers.extend(additional_numbers) method extends numbers with elements in additional_numbers. This allows you to append multiple elements to a list in one go. Compare this to the .append() method which only adds one element to a list.

Note that numbers is the one modified directly in this case; additional_numbers is left untouched.

Also contrast this to [3, 4, 5] + [1, 2]. List concatenation will produce a new list, rather than modifying an existing list.

Question 10

What is the value of item and numbers after executing the following code?

1
2
numbers = [3, 4, 5]
item = numbers.pop(0)
3
[4, 5]
Explanation:

The .pop() method does two things. It removes an element from the list at a specified position (or removes the last item if the position is not specified). At the same time, it also returns the element that was removed. In our example, the first element (at index 0) is removed from numbers, and its value (3) returned. This is different from numbers[0] which only returns the value without modifying numbers, and also different from del numbers[0] which only deletes an element without returning anything.