A Python list is an ordered, changeable sequence of items. It is one of the most commonly used data structures in Python.
In this tutorial, you’ll learn how to create a list, access its items, and add, modify, and remove items. You’ll also learn how to iterate, sort, copy, and search lists, and how to use nested lists and list comprehensions, with examples.

A list has the following properties:
- Mutable: You can change, add, or remove items after the list is created.
- Ordered: Items keep the order in which you add them, and each item has an index position. New items are added at the end by default.
- Heterogeneous: A list can hold items of different types, such as strings, integers, booleans, or even other lists.
- Allows duplicates: A list can contain the same value more than once.
- Dynamic: A list grows and shrinks automatically as you add or remove items.
Why use a list?
- Lists are very flexible and come with many built-in methods, such as
append(),pop(), andsort(), which make it easy to work with data that keeps changing. - Because lists are mutable, they are a good choice when the values in a collection change frequently.
- They can store any mix of values, including duplicates, while keeping them in order.
Table of contents
- Creating a Python list
- Length of a list
- Accessing items of a list
- Iterating a list
- Adding elements to the list
- Modify the items of a list
- Removing elements from a list
- Finding an element in the list
- Concatenation of two lists
- Copying a list
- List operations
- Python built-in functions with lists
- Nested list
- List comprehension
- Common mistakes with lists
- Summary of list operations
Creating a Python list
You can create a list in two ways:
- Using square brackets (
[]): Write the items inside square brackets, separated by commas. This is the most common way. - Using the
list()constructor: Pass any iterable, such as a tuple, string, orrange(), tolist(), and it builds a list from its items.
Length of a list
To find the number of items in a list, use the built-in len() function.
Accessing items of a list
You can access list items in two ways:
- Indexing: get a single item using its index number.
- Slicing: get a range of items.
Indexing
Every item in a list has an index (position number). You can use this index inside square brackets to get the item.

Indexes go from left to right, starting at 0 for the first item and ending at length of the list – 1 for the last. So to get the 3rd item, use index 2.
Note:
- Valid indexes go from 0 to
len(list) - 1. - If you use an index that is outside this range, Python raises an
IndexError. - Indexes must be integers. Using any other type, such as a float or a string, raises a
TypeError.
Example
We accessed the second item using index 1 and the fifth item using index 4.
Negative indexing
You can also access items from right to left using negative indexes. The last item has index -1, the second-to-last has -2, and so on down to -len(list) for the first item.
List slicing
Slicing lets you get a range of items from a list as a new list. For example, you can get the items from position 3 to 7. You can also use slicing to change a range of items at once.
The syntax for list slicing is:
list_name[start_index : end_index : step]Code language: Python (python)
start_indexis the position where the slice begins (included). If omitted, it defaults to 0.end_indexis the position where the slice stops (not included). If omitted, the slice goes to the end of the list.steplets you take every nth item in the range. The default is 1. A negative step goes backward.
Example
Let’s see a few more slicing examples:
- Extract the first part of a list
- Slice with a step
- Reverse a list
- Slice without an end position
Example
Iterating a list
You can go through the items of a list one by one using a for loop.
Output
5 8 Tom 7.5 Emma
Iterate along with an index number
When you need both the index and the value, the easiest way is the built-in enumerate() function. In each iteration, it gives you the index and the item together.
Output
0 apple 1 banana 2 cherry
You can also loop over the index numbers with range() and len(). Since indexes go from 0 to len(list) - 1, range(len(my_list)) produces exactly the indexes you need.
Example
Output
5 8 Tom 7.5 Emma
Adding elements to the list
You can add a new item, or several items, to a list using the methods append(), insert(), and extend().
Append item at the end of the list
The append() method takes exactly one argument and adds it to the end of the list.
Let’s add the item ‘Emma’ to the end of the list.
Notice that appending a list adds it as a single item (a nested list). To add each item separately, use extend().
Add item at the specified position in the list
Use the insert() method to add an item at a specific position. It takes two arguments: the index and the item.
list_name.insert(index, item)Code language: Python (python)
It inserts the item before the current item at index, shifting the rest to the right.
As you can see, item 25 was added at index position 2.
Using extend()
The extend() method takes an iterable (such as another list) and adds each of its items to the end of the list.
Let’s add three items to the end of the list.
We added three integers at once. They are added in the same order they were passed, at the end of the list.
Modify the items of a list
Lists are mutable, so you can change their items. Use the index number and the assignment operator (=) to give an item a new value.
Let’s see two scenarios:
- Modify a single item
- Modify a range of items
Modify all items
Use a for loop with the index to modify every item in place. Let’s replace each item with its square.
Alternatively, build a new list with a list comprehension: my_list = [x * x for x in my_list].
Removing elements from a list
You can remove items from a list using the following methods and statements.
| Method | Description |
|---|---|
remove(item) | Removes the first occurrence of the item from the list. |
pop(index) | Removes and returns the item at the given index. Without an index, it removes the last item. |
clear() | Removes all items, leaving an empty list. |
del list_name[index] | Deletes the item at an index, or a slice of items. |
del list_name | Deletes the entire list variable. |
Remove a specific item
Use the remove() method to remove the first occurrence of an item from the list.
Note: It raises a ValueError if the item is not present in the list.
Example
Remove all occurrences of a specific item
Since remove() deletes only the first match, you need a different approach to remove every occurrence. The clearest way is a list comprehension that keeps all the other items:
Warning: Don’t call remove() inside a for loop that iterates over the same list. Removing items shifts the remaining ones, so the loop can skip items and give wrong results.
Remove the item at a given index
Use the pop() method to remove the item at a given index. pop() also returns the removed item, so you can use it.
Note: If you don’t pass an index, pop() removes the last item in the list.
Example
Remove a range of items
Use the del keyword with slicing to remove a range of items.
Remove all items
Use the list’s clear() method to remove all items. The list still exists, but it is empty. To delete the list variable itself, use del.
Finding an element in the list
To check whether an item exists in a list, use the in operator. To get the position of an item, use the index() method. To count how many times an item appears, use count().
index() returns the index of the first occurrence of the item, or raises a ValueError if the item doesn’t exist.
Tip: Check with in before calling index() to avoid the error.
Concatenation of two lists
Concatenating two lists means joining them together. There are two ways to do that:
- The
+operator creates a new list that contains the items of both lists. - The
extend()method adds the items of the second list to the end of the existing first list.
You can also repeat a list with the * operator, for example, [0] * 3 gives [0, 0, 0].
Example
Copying a list
Copying a list is a common source of bugs, because the assignment operator doesn’t actually make a copy. Let’s compare the options.
Using the assignment operator (=)
When you write new_list = my_list1, Python does not create a new list. Both names refer to the same list object, so any change made through one name is visible through the other.
As you can see, the change to the original list also shows up in new_list, because they are the same list. So don’t use the assignment operator when you need an independent copy. Use the copy() method instead.
Using the copy() method
The copy() method creates a new list with the same items. Changes to the original list (such as adding or removing items) don’t affect the copy. This is called a shallow copy. You can also make a shallow copy with my_list[:] or list(my_list).
This time, the change to the original list is not reflected in the copy.
Deep copy
A shallow copy creates a new outer list, but any nested lists inside it are still shared. If your list contains other lists, use copy.deepcopy() to copy everything, including the nested lists.
List operations
Lists have built-in methods for common operations such as sorting and reversing.
Sort a list using sort()
The sort() method sorts the items of the list in place, in ascending order by default.
Output
[1, 2, 3]
Useful options and alternatives:
sort(reverse=True)sorts in descending order.sort(key=len)sorts by a custom rule, here by the length of each item.- The built-in
sorted()function returns a new sorted list and leaves the original unchanged.
Reverse a list using reverse()
The reverse() method reverses the order of the items in place.
Output
[1, 6, 5, 4, 3]
The items are now in reverse order. Note that reverse() doesn’t sort the list. It only flips the current order.
Python built-in functions with lists
Besides the list’s own methods, you can use many of Python’s built-in functions on lists. Let’s look at a few of them.
Using max() & min()
The max() function returns the largest value in the list, and min() returns the smallest.
Output
6 1
Using sum()
The sum() function returns the total of all the numbers in the list.
Output
19
all()
The all() function returns True only when every item in the list is true (or the list is empty). Here are the possible cases:
| Item values in list | Return value |
|---|---|
| All values are true | True |
| One or more false values | False |
| All values are false | False |
| Empty list | True |
Output
all() All True values: True all() with One false value: False all() with all false values: False all() Empty list: True
any()
The any() function returns True if at least one item in the list is true. For an empty list, it returns False.
| Item values in list | Return value |
|---|---|
| All values are true | True |
| One or more false values | True |
| All values are false | False |
| Empty list | False |
Let’s see each of these cases with a small example.
Output
any() True values: True any() One false value: True any() all false values: False any() Empty list: False
Nested list
A list can contain other lists (sub-lists), which can contain more lists, and so on. This is called a nested list.
To access an item of an inner list, use two indexes: the first selects the inner list, and the second selects the item inside it. To go through all the items of the inner lists, use a nested for loop.
Output
Accessing the third element of the second list 5 list [2, 4, 6, 8, 10] elements 2 4 6 8 10 list [1, 3, 5, 7, 9] elements 1 3 5 7 9
In nested_list[1][2], the first index (1) selects the second inner list, and the second index (2) selects its third item.
List comprehension
A list comprehension is a short, readable way to create a new list from an existing iterable. It can also filter the items, keeping only those that satisfy a condition.
new_list = [expression for item in iterable if condition]Code language: Python (python)
expression: Required. Computes each item of the new list, for example,item ** 2.item: Required. A variable that takes each value from the iterable in turn.iterable: Required. The input sequence, such as a list orrange().if condition: Optional. Keeps only the items for which the condition is true.
Output
[16, 324, 400]
We created a new list from an existing list in a single statement. It contains only the squares of the even numbers from the input list.
The input can also be a range of numbers:
Output
[0, 4, 16, 36, 64]
Common mistakes with lists
- Expecting
=to copy a list: It only creates another name for the same list. Usecopy()orcopy.deepcopy(). - Assigning the result of
sort():sort(),append(), andreverse()change the list in place and returnNone. Writingmy_list = my_list.sort()leaves you withNone. Usesorted()if you need a new list. - Removing items while looping over the list: This skips items. Build a new list with a comprehension instead.
- Creating a 2D list with
*:[[0] * 3] * 3creates three references to the same inner list, so changing one row changes them all. Use[[0] * 3 for _ in range(3)]instead. - Index out of range: The last valid index is
len(list) - 1. Usemy_list[-1]to get the last item safely.
Summary of list operations
For the following examples, assume that l1 and l2 are lists and x, i, j, and k are integers.
l1 = [10, 20, 30, 40, 50] and l2 = [60, 70, 80, 60]
| Operation | Description |
|---|---|
x in l1 | Check if the list l1 contains item x. |
x not in l2 | Check if the list l2 does not contain item x. |
l1 + l2 | Concatenate the lists l1 and l2. Creates a new list containing the items of both. |
l1 * 5 | Repeat the list l1 5 times. |
l1[i] | Get the item at index i. For example, l1[2] is 30. |
l1[i:j] | List slicing. Get the items from index i up to index j (excluding j) as a list. For example, l1[0:2] is [10, 20]. |
l1[i:j:k] | List slicing with a step. Returns the items from index i up to index j, taking every k-th item. For example, l1[0:4:2] is [10, 30]. |
len(l1) | Returns the total number of items in the list. Here, 5. |
l2.count(60) | Returns the number of times an item (60) appears in the list. The answer is 2. |
l1.index(30) | Returns the index of the first occurrence of an item (30). The answer is 2. |
l1.index(30, 2, 5) | Returns the index of an item (30), searching only from index 2 up to index 5. The answer is 2. |
min(l1) | Returns the item with the smallest value. The answer is 10. |
max(l1) | Returns the item with the largest value. The answer is 50. |
sum(l1) | Returns the total of all items. The answer is 150. |
l1.append(100) | Add an item at the end of the list. |
l1.append([2, 5, 7]) | Append a nested list at the end. |
l1.extend([2, 5, 7]) | Add each item of another list at the end. |
l1.insert(1, 15) | Insert item 15 at index 1. |
l1[2] = 40 | Modify the item at index 2. |
l1.remove(40) | Remove the first occurrence of item 40 from the list. |
l1.pop(2) | Remove and return the item at index 2. |
l1.clear() | Make the list empty. |
l1.sort() / sorted(l1) | Sort the list in place / return a new sorted list. |
l1.reverse() | Reverse the list in place. |
l3 = l1.copy() | Create a shallow copy of l1 in l3. |
