Tutorials › Python Tutorial › Python List Methods and Slicing

Python List Methods and Slicing

12 min read · Lesson 11 of 24

In Python Lists you learned to create lists and read their items. Now you will change them: add items, remove items, sort them, copy them and take slices out of them.

Adding items

MethodWhat it does
append(x)adds x to the end
insert(i, x)inserts x at position i
extend(other)adds every item from another list
Example PYTHON
queue = ["Achieng", "Barasa"]
queue.append("Chepkoech")
print(queue)
queue.insert(0, "Daudi")
print(queue)
queue.extend(["Esther", "Fatuma"])
print(queue)
Result
['Achieng', 'Barasa', 'Chepkoech']
['Daudi', 'Achieng', 'Barasa', 'Chepkoech']
['Daudi', 'Achieng', 'Barasa', 'Chepkoech', 'Esther', 'Fatuma']
Try it Yourself »
Note: List methods like append() change the list in place and return None. Do not write queue = queue.append("x") – that would replace your list with None.

Removing items

Method / statementWhat it does
remove(x)removes the first item equal to x
pop()removes and returns the last item
pop(i)removes and returns the item at index i
del lst[i]deletes the item at index i
clear()removes everything
Example PYTHON
cart = ["sugar", "rice", "salt", "rice", "oil"]
cart.remove("rice")
print(cart)
last = cart.pop()
print("Popped:", last)
first = cart.pop(0)
print("Popped:", first)
del cart[0]
print(cart)
cart.clear()
print(cart)
Result
['sugar', 'salt', 'rice', 'oil']
Popped: oil
Popped: sugar
['rice']
[]
Try it Yourself »

Notice that remove("rice") only removed the first "rice".

Finding items

Example PYTHON
votes = ["Yes", "No", "Yes", "Yes", "No"]
print(votes.count("Yes"))
print(votes.index("No"))
Result
3
1
Try it Yourself »

index() gives the position of the first match. If the item is not in the list, it raises a ValueError, so check with in first if you are not sure.

Sorting and reversing

sort() arranges the list in place. Add reverse=True for descending order. reverse() simply flips the current order.

Example PYTHON
scores = [64, 91, 47, 78]
scores.sort()
print(scores)
scores.sort(reverse=True)
print(scores)

towns = ["Voi", "Athi River", "Kitui"]
towns.reverse()
print(towns)
Result
[47, 64, 78, 91]
[91, 78, 64, 47]
['Kitui', 'Athi River', 'Voi']
Try it Yourself »

If you want a sorted copy and want to keep the original as it is, use the built-in sorted() function instead:

Example PYTHON
prices = [350, 120, 900]
cheap_first = sorted(prices)
print(cheap_first)
print(prices)
Result
[120, 350, 900]
[350, 120, 900]
Try it Yourself »

Sorting with a key

The key argument tells Python what to sort by. Here we sort names by their length:

Example PYTHON
names = ["Wairimu", "Ali", "Nekesa", "Omondi"]
names.sort(key=len)
print(names)
Result
['Ali', 'Nekesa', 'Omondi', 'Wairimu']
Try it Yourself »

Slicing

A slice takes a section of a list using list[start:end]. The item at start is included; the item at end is not.

Example PYTHON
months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
print(months[1:4])
print(months[:3])
print(months[3:])
print(months[-2:])
Result
['Feb', 'Mar', 'Apr']
['Jan', 'Feb', 'Mar']
['Apr', 'May', 'Jun']
['May', 'Jun']
Try it Yourself »

Slicing with a step

A third number, the step, skips items. A step of -1 walks backwards, which gives a reversed copy.

Example PYTHON
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(numbers[::2])
print(numbers[1::3])
print(numbers[::-1])
Result
[0, 2, 4, 6, 8]
[1, 4, 7]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Try it Yourself »
Tip: Slicing never raises an IndexError. months[4:100] just returns what is there. That makes slices safe for "first few items" tasks.

Copying a list

Assigning a list to a new name does not copy it. Both names point to the same list, so changing one changes the other.

Example PYTHON
original = ["tea", "coffee"]
same = original
same.append("juice")
print(original)

copy = original.copy()
copy.append("water")
print(original)
print(copy)
Result
['tea', 'coffee', 'juice']
['tea', 'coffee', 'juice']
['tea', 'coffee', 'juice', 'water']
Try it Yourself »

Use .copy(), list(original) or original[:] to get an independent copy.

Joining lists

The + operator joins two lists into a new one, and * repeats a list.

Example PYTHON
east = ["Kenya", "Uganda"]
more = ["Tanzania", "Rwanda"]
print(east + more)
print([0] * 4)
Result
['Kenya', 'Uganda', 'Tanzania', 'Rwanda']
[0, 0, 0, 0]
Try it Yourself »

Exercises

Exercise

Add "Kitale" to the end of the list.

towns.("Kitale")
Exercise

Slice the first three items of the list.

print(towns[:])

Next: Python Tuples and Sets.

Sign in or create a free account to track progress, earn XP, badges and a certificate.