Python lists can store an ordered collection of items, or elements, of varying types. They are often used to compile multiple items into a single, mutable variable, which is helpful for retrieving items, specifying outputs or performing calculations quickly. Lists are also a type of built-in data structure in Python (along with tuples, sets and dictionaries), which is a specified way of storing and formatting data.
8 Ways to Modify a Python List
- Index method
- Count method
- Sort method
- Append method
- Remove method
- Pop method
- Extend method
- Insert method
If you’re curious about using lists in Python, here’s how to create one and modify them in different ways.
How to Create a List in Python
To create a list in Python, write a set of items within square brackets ([]) and separate each item with a comma. Items in a list can be any basic object type found in Python, including integers, strings, floating point values or boolean values.
For example, to create a list named “z” that holds the integers 3, 7, 4 and 2, you would write:
# Define a list
z = [3, 7, 4, 2]
The “z” list defined above has items that are all of the same type (integer or int), but as mentioned, 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]
This list contains an integer (int), a bool, a string and a float.
Features of Lists in Python
All Python lists include the following features or characteristics:
- Lists can contain items of different types at the same time (including strings, integers, floating point numbers and boolean values).
- Lists are mutable and dynamic; list items can be added, removed or changed after the list is defined.
- Lists are ordered; newly added items will be placed at the end of the list.
- Lists use zero-based indexing; every list item has an associated index, and the first item’s index is 0.
- Duplicated list items are allowed.
- Lists can be nested within other lists indefinitely.
How to Access Values in a Python 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 starts at index 0 and ascends accordingly. In the example list, “z,” the indices of each list item would look like this:
To access an item value, print the index of the associated item in the list. As an example, say you wanted to access the first item from the list “z,” shown in blue below:
To access this item, you would use the item’s index, 0, and write:
# Define a list
z = [3, 7, 4, 2]
# Access the first item of a list at index 0
print(z[0])
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.
# print last item in the list
print(z[-1])
As a reminder, you could also access the same item using positive indexes, as seen below.
How to Slice a Python 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.
# Define a list
z = [3, 7, 4, 2]
print(z[0:2])
# everything up to but not including index 3
print(z[:3])
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:])
How to Update an Item in a Python List
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)
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.
1. Index Method
The index method returns the first index at which a value occurs. Say you want to get the index of the first occurrence of “4” in this list:
# Define a list
z = [4, 1, 5, 4, 10, 4]
In the code below, using index() will return 0.
print(z.index(4))
You can also specify where you want to start your search if there are multiple of the same list item.
print(z.index(4, 3))
2. 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)
3. Sort Method
The sort method sorts and alters the original list in place.
z = [3, 7, 4, 2]
z.sort()
print(z)
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.
# Sorting and Altering original list
# high to low
z.sort(reverse = True)
print(z)
As an aside, I should mention that you can also sort a list of strings from “a-z” and “z-a”.
4. Append Method
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)
5. Remove Method
The remove method removes the first occurrence of a value in a list.
z = [7, 4, 3, 2, 3]
z.remove(2)
print(z)
Code removes the first occurrence of the value 2 from the list z.
6. Pop Method
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)
7. Extend Method
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)
Alternatively, the same thing could be accomplished by using the +
operator.
print([1,2] + [3,4])
8. Insert Method
The insert method inserts an item before the index you provide.
z = [7, 3, 3, 4, 5]
z.insert(4, [1, 2])
print(z)
Frequently Asked Questions
What are lists in Python?
Lists are a type of data structure in Python that can store an ordered collection of items, or elements, of varying types.
What is the syntax for lists in Python?
The syntax for lists in Python is represented by square brackets, or [ ].
How do lists work in Python?
Lists in Python are ordered, mutable and can contain items of different types at the same time. Each item in a Python list also has an associated index, where the first item's index is 0 and ascends accordingly for each following item.
How to create a Python list?
To create a Python list, enclose a set of items in square brackets ([ ]), separate each item with a comma, and assign the list to a variable.
As an example, this would look like:
example_list = [1, 2, 3, 4]