Python List Methods

About This Index
This page is the Python List Methods index for the tutorial track. Each row links to a full guide with syntax, parameters, return value, three runnable examples, and FAQs. Every example uses fixed data, and since a list keeps its items in order, the output you see is the output you get.
Learn indexing, slicing, creating lists, and the classic mistakes on the List hub. Use this page when you need a specific method such as append(), pop(), or sort().
Every method below belongs to the built-in list type, so no import is needed. Eight of them change the list in place; of those, only pop() hands something back, and the rest return None. The Returns column tells you which is which.
How to Call a List Method
Write the list, a dot, the method name, and the arguments. Methods that change the list return None, so call them on their own line; pop(), index(), count(), and copy() give you a value to use:
nums = [5, 3, 8]
nums.append(1) # add one item at the end
nums.insert(0, 9) # add one item at position 0
print(nums) # [9, 5, 3, 8, 1]
print(nums.pop()) # 1
print(nums.index(8)) # 3
nums.sort() # sorts in place, returns None
print(nums) # [3, 5, 8, 9]
print(nums.count(5)) # 1 Python List Methods Table
All method guides in this Python tutorial series (11 tutorials), grouped by purpose.
Add items
3 methodsPut one item at the end, one item at any position, or every item of another iterable at the end.
Remove items
3 methodsTake an item out by position and get it back, delete the first item equal to a value, or empty the whole list in place.
Search and count
2 methodsFind the position of the first matching item, or count how many items are equal to a value.
Order and copy
3 methodsSort in place with an optional key, flip the order in place, or make a new shallow copy.
List Operators and Idioms
These are not named methods, but you will use them next to the methods above in almost every program:
| Code | What it does |
|---|---|
lst[i] | Item at position i, counting from 0; a negative i counts from the end. Raises IndexError when out of range. |
lst[a:b:c] | New list from index a up to, but not including, b, taking every c-th item. Out-of-range bounds never raise. |
lst[i] = x | Replace the item at position i. |
del lst[i] | Remove the item at i (or a slice, del lst[a:b]) without returning it. |
x in lst | True if any item is equal (==) to x. |
len(lst) | Number of items. |
lst + other | New list with the items of both; other must also be a list. |
lst * n | New list with the items repeated n times; the items themselves are shared, not copied. |
lst += iterable | Extend in place, exactly like extend(); accepts any iterable, such as a tuple. |
sorted(lst) | New sorted list; lst.sort() sorts the list itself and returns None. |
reversed(lst) | Iterator that walks the list backward; lst.reverse() flips the list itself. |
lst[::-1] | New list in reverse order; the original is unchanged. |
a = [10, 20, 30, 40, 50]
print(a[1], a[-1]) # 20 50
print(a[1:4], a[::2]) # [20, 30, 40] [10, 30, 50]
del a[0]
print(30 in a, len(a)) # True 4
print(a + [60], [0] * 3) # [20, 30, 40, 50, 60] [0, 0, 0]
print(sorted(a, reverse=True)) # [50, 40, 30, 20]
print(list(reversed(a)) == a[::-1]) # True
a += (70,) # same as a.extend((70,))
print(a) # [20, 30, 40, 50, 70] Pick sort() or reverse() when you want to change the list itself, and sorted(), reversed(), or [::-1] when the original must stay as it is. a + [60] needs a list on both sides, while a += (70,) and extend() accept any iterable.
Frequently Asked Questions
Start with append()
Open the first guide in the list and work your way through the list methods.
16 people found this page helpful
