9.4. List Methods

As with strings, Python provides us with useful methods for lists. These methods will either change an existing list, return information about the list, or create and return a new list.

9.4.1. Common List Methods

Here is a sample of the most frequently used list methods. More complete lists can be found here:

  1. W3Schools

  2. Python.org

To see detailed examples for a particular method, click on its name.

Methods That Return Information About The List

Method

Syntax

Description

count

list_name.count(value)

Returns the number of elements in the list that match value.

index

list_name.index(value)

Returns the index of the FIRST occurrence of value in the list. If the value is not in the list, Python throws an error.

Methods That Rearrange The Entries In The list

Method

Syntax

Description

reverse

list_name.reverse()

Reverses the order of the elements in a list.

sort

list_name.sort()

Arranges the elements of a list into increasing or decreasing order.

Methods That Add Or Remove Entries From A List

Method

Syntax

Description

append

list_name.append(value)

Adds value to the end of the list.

clear

list_name.clear()

Removes all elements from a list.

insert

list_name.insert(index, value)

Adds value at the specified index in the list. This pushes existing elements further down the list.

pop

list_name.pop(index)

Removes and returns the element at the given index value. If no index is provided, the last element in the list gets removed.

remove

list_name.remove(value)

Removes the FIRST element in a list that matches value.

Methods That Create New Lists

Method

Syntax

Description

join

'connecter'.join(list_name)

Combines all the elements of a list into a string.

split

'string'.split('delimiter')

Divides a string into smaller pieces, which are stored as separate elements in a new list.

list

list(collection)

This is a function rather than a method, and it behaves similarly to data conversion functions like int() and str(). The list() function tries to convert the whatever is inside the () into a list.

9.4.2. Check Your Understanding

As you answer these questions, follow the links in the table above as needed.

Question

What is printed by the following code?

1
2
3
string_list = ['coder', 'Tech', '47', '23', '350']
string_list.sort()
print(string_list)
  1. [‘350’, ‘23’, ‘47’, ‘Tech’, ‘coder’]

  2. [‘coder’, ‘Tech’, ‘23’, ‘47’, ‘350’]

  3. [‘23’, ‘47’, ‘350’, ‘coder’, ‘Tech’]

  4. [‘23’, ‘350’, ‘47’, ‘Tech’, ‘coder’]

Question

Which statement converts the string text = 'Coding students rock!' into the list ['Coding', 'students', 'rock!']?

  1. text.join()

  2. text.split()

  3. text.join("")

  4. text.split("")

  5. list(text)

Question

What is printed by the following program?

1
2
3
4
5
6
grocery_bag = ['bananas', 'apples', 'edamame', 'chips', 'cucumbers', 'milk', 'cheese']
selected_items = []

selected_items = grocery_bag[2:5]
selected_items.sort()
print(selected_items)
  1. [‘chips’, ‘cucumbers’, ‘edamame’]

  2. [‘chips’, ‘cucumbers’, ‘edamame’, ‘milk’]

  3. [‘apples’, ‘chips’, ‘edamame’]

  4. [‘apples’, ‘chips’, ‘cucumbers’, ‘edamame’]

Question

What is printed by the following program?

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

  2. [4, 2, 8, 6, 5, True, False]

  3. [True, False, 4, 2, 8, 6, 5]

Question

What is printed by the following program?

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

  2. [4, False, True, 2, 8, 6, 5]

  3. [False, 2, True, 6, 5]

Question

What is printed by the following program?

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

  2. [2, 6, 5]

  3. [4, 2, 6]