In Python, lists are written within square brackets [].

Defining a list. The second row in this table index is how you access items in the list.
Defining a list. The second row in this table index is how you access items in the list. | Image: Michael Galarnyk
# Define a list
z = [3, 7, 4, 2]

8 Ways to Modify a Python List

  1. Index method
  2. Count method
  3. Sort method
  4. Append method
  5. Remove method
  6. Pop method
  7. Extend method
  8. Insert method

Lists store an ordered collection of items which can be of different types. The list defined above has items that are all of the same type (int), but all the items in a list do not need to be of the same type as you can see below.

# Define a list
heterogenousElements = [3, True, 'Michael', 2.0]

The list contains an integer (int), a bool, a string and a float.

 

How to Access Values in a List

Each item in a list has an assigned index value. It’s important to note that Python is a zero indexed based language. All this means is that the first item in the list is at index 0.

Access item at index 0 (in blue).
Access item at index 0 (in blue). | Image: Michael Galarnyk
# Define a list
z = [3, 7, 4, 2]
# Access the first item of a list at index 0
print(z[0])
Output of accessing the item at index 0.
Output of accessing the item at index 0. | Image: Michael Galarnyk

Python also supports negative indexing. Negative indexing starts at the end. It can be more convenient at times to use negative indexing to get the last item in the list because you don’t have to know the length of the list to access the last item.

Accessing the item at the last index.
Accessing the item at the last index. | Image: Michael Galarnyk
# print last item in the list
print(z[-1])
Output of accessing the last item in the List.
Output of accessing the last item in the List. | Image: Michael Galarnyk

As a reminder, you could also access the same item using positive indexes, as seen below.

Alternative way of accessing the last item in the list z.
Alternative way of accessing the last item in the list z. | Image: Michael Galarnyk

 

How to Slice a List

Slices are good for getting a subset of values in your list. For the example code below, it will return a list with the items from index 0 up to and not including index 2.

First index is inclusive before the :, and the last after the : is not. | Image: Michael Galarnyk
First index is inclusive before the :, and the last after the : is not. | Image: Michael Galarnyk
# Define a list
z = [3, 7, 4, 2]
print(z[0:2])
Slice of a list syntax.
Slice of a list syntax. | Image: Michael Galarnyk
A sliced list.
A sliced list. | Image: Michael Galarnyk
# everything up to but not including index 3
print(z[:3])
Printing a sliced list.
Printing a sliced list. | Image: Michael Galarnyk
A sliced list printed.
A sliced list printed. | Image: Michael Galarnyk

The code below returns a list with items from index 1 to the end of the list

# index 1 to end of list
print(z[1:])
Printing a sliced list at index 1. | Image: Michael Galarnyk
Printing a sliced list at index 1. | Image: Michael Galarnyk

 

Update an Item in a List

Updating an item in a list.
Updating an item in a list. | Image: Michael Galarnyk

Lists in Python are mutable. After defining a list, it’s possible to update the individual items in a list.

# Defining a list
z = [3, 7, 4, 2]
# Update the item at index 1 with the string "fish"
z[1] = "fish"
print(z)
Code to modify an item in a list.
Code to modify an item in a list. | Image: Michael Galarnyk

More on Python: How to Write Nested List Comprehensions in Python

 

Python List Modification Methods

Python lists have different methods that help you modify a list. This section of the tutorial just goes over various python list methods.

 

Index Method

Modifying a list with the index method.
Modifying a list with the index method. | Image: Michael Galarnyk
# Define a list
z = [4, 1, 5, 4, 10, 4]
Modifying a list with the index method at index 4.
Modifying a list with the index method at index 4. | Image: Michael Galarnyk

The index method returns the first index at which a value occurs. In the code below, it will return 0.

print(z.index(4))
Printing a indexed list at index 1.
Printing an index list. | Image: Michael Galarnyk
Modifying a list with the index method.
Modifying a list with the index method. | Image: Michael Galarnyk

You can also specify where you want to start your search.

print(z.index(4, 3))
Printing an index from a start point.
Printing an index from a start point. | Image: Michael Galarnyk

 

Count Method

Just like how it sounds, the count method counts the number of times a value occurs in a list

random_list = [4, 1, 5, 4, 10, 4]
random_list.count(5)
Modifying a list with the count method.
Modifying a list with the count method. | Image: Michael Galarnyk

 

Sort Method

Sort a Python List. the actual code would be: z.sort().
Sort a Python List. the actual code would be: z.sort(). | Image: Michael Galarnyk

The sort method sorts and alters the original list in place.

z = [3, 7, 4, 2]
z.sort()
print(z)
Sorting a list and printing z.
Sorting a list and printing z. | Image: Michael Galarnyk

The code above sorts a list from low to high. The code below shows that you can also sort a list from high to low.

Sort a python list from high to low.
Sort a python list from high to low. | Image: Michael Galarnyk
# Sorting and Altering original list
# high to low
z.sort(reverse = True)
print(z)
Results of sorting a list in reverse.
Results of sorting a list in reverse. | Image: Michael Galarnyk

As an aside, I should mention that you can also sort a list of strings from “a-z” and “z-a” as you can see below.

A tutorial on Python list and list manipulation. | Video: Michael Galarnyk

More on Python: PCA Using Python: A Tutorial

 

Append Method

Add the value 3 to the end of the list.
Add the value 3 to the end of the list. | Image: Michael Galarnyk

The append method adds an element to the end of a list. This happens in place.

z = [7, 4, 3, 2]
z.append(3)
print(z)
Appended list result.
Appended list result. | Image: Michael Galarnyk

 

Remove Method

Remove method in lists.
Remove method in lists. | Image: Michael Galarnyk

The remove method removes the first occurrence of a value in a list.

z = [7, 4, 3, 2, 3]
z.remove(2)
print(z)
Removing 2 from an index.
Removing value 2 from index z. | Image: Michael Galarnyk

Code removes the first occurrence of the value 2 from the list z

 

Pop Method

z.pop(1) removes the value at index 1 and returns the value 4.
z.pop(1) removes the value at index 1 and returns the value 4. | Image: Michael Galarnyk

The pop method removes an item at the index you provide. This method will also return the item you removed from the list. If you don’t provide an index, it will default to removing the item at the last index.

z = [7, 4, 3, 3]
print(z.pop(1))
print(z)
Print method in Python lists.
Print method in Python lists. | Image: Michael Galarnyk

 

Extend Method

Extend method in Python lists.

This method extends a list by appending items. The benefit of this is you can add lists together.

z = [7, 3, 3]
z.extend([4,5])
print(z)
Add the list [4, 5] to the end of the list z.
Add the list [4, 5] to the end of the list z. | Image: Michael Galarnyk

Alternatively, the same thing could be accomplished by using the + operator.

print([1,2] + [3,4])
Results of an extended list in Python.
Results of an extended list in Python. | Image: Michael Galarnyk

More on Python: Python Dictionary and Dictionary Methods: A Guide

 

Insert Method

Insert the list [1,2] at index 4.
Insert the list [1,2] at index 4. | Image: Michael Galarnyk

The insert method inserts an item before the index you provide.

z = [7, 3, 3, 4, 5]
z.insert(4, [1, 2])
print(z)
Insert method in Python lists.
Insert method in Python lists. | Image: Michael Galarnyk
Expert Contributors

Built In’s expert contributor network publishes thoughtful, solutions-oriented stories written by innovative tech professionals. It is the tech industry’s definitive destination for sharing compelling, first-person accounts of problem-solving on the road to innovation.

Learn More

Great Companies Need Great People. That's Where We Come In.

Recruit With Us