Sorting, slicing and searching lists
The last lesson built and changed lists. This one is about putting them in order, cutting pieces from them and finding things in them.
Sorting
There are two ways to sort, and the difference matters.
marks = [78, 45, 91, 60]
marks.sort()
print(marks)
names = ["Ravi", "Asha", "Meena"]
ordered = sorted(names)
print(ordered)
print(names)Output
[45, 60, 78, 91]
['Asha', 'Meena', 'Ravi']
['Ravi', 'Asha', 'Meena']- The method
list.sort()rearranges the list itself and returns nothing. - The function
sorted(list)leaves the original alone and returns a new sorted list.
Use sorted when you still need the original order. Use .sort() when you do not.
For largest first, add reverse=True.
marks = [78, 45, 91, 60]
marks.sort(reverse=True)
print(marks)
print(sorted(["pear", "apple", "fig"], reverse=True))Output
[91, 78, 60, 45]
['pear', 'fig', 'apple']Text is sorted alphabetically, but capital letters come before all small letters. To ignore case, pass key=str.lower. A key is a function that Python applies to each item just for the comparison.
words = ["banana", "Apple", "cherry"]
print(sorted(words))
print(sorted(words, key=str.lower))
print(sorted(words, key=len))Output
['Apple', 'banana', 'cherry']
['Apple', 'banana', 'cherry']
['Apple', 'banana', 'cherry']The last line sorts by length, shortest first.
Reversing
order = [1, 2, 3, 4]
order.reverse()
print(order)
print(order[::-1])Output
[4, 3, 2, 1]
[1, 2, 3, 4]reverse turns the list around in place. The slice [::-1] gives a reversed copy.
Slices
Slicing a list works like slicing a string and always gives a new list.
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
print(days[:5])
print(days[5:])
print(days[1:4])
print(days[::2])
print(days[-3:])Output
['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
['Sat', 'Sun']
['Tue', 'Wed', 'Thu']
['Mon', 'Wed', 'Fri', 'Sun']
['Fri', 'Sat', 'Sun']days[:5] is the first five items. days[-3:] is the last three. These two are worth remembering: top five and bottom three of a sorted list are one slice each.
scores = [55, 92, 71, 88, 64, 97]
top3 = sorted(scores, reverse=True)[:3]
print(top3)Output
[97, 92, 88]Finding things
names = ["Asha", "Ravi", "Meena", "Ravi"]
print("Meena" in names)
print("Kabir" not in names)
print(names.index("Ravi"))
print(names.count("Ravi"))Output
True
True
1
2in answers yes or no. index gives the position of the first match. count says how many times a value appears. index raises an error if the value is missing, so check with in first when you are not sure.
Smallest, largest, average
temps = [31, 29, 35, 33, 30]
print(min(temps), max(temps))
print(sum(temps) / len(temps))
print(temps.index(max(temps)))Output
29 35
31.6
2The last line finds where the hottest day sits in the list.
Two lists side by side
zip pairs up the items of two lists, first with first, second with second.
names = ["Asha", "Ravi", "Meena"]
marks = [78, 91, 66]
for name, mark in zip(names, marks):
print(f"{name:<6}{mark}")Output
Asha 78
Ravi 91
Meena 66If one list is longer, zip stops at the end of the shorter one.
Joining and repeating lists
a = [1, 2]
b = [3, 4]
print(a + b)
print(a * 3)
print([0] * 5)Output
[1, 2, 3, 4]
[1, 2, 1, 2, 1, 2]
[0, 0, 0, 0, 0][0] * 5 is a quick way to make a list of five zeros to fill in later.
From text to list and back
split and join from the string lesson are the bridge between text and lists.
line = "tea, coffee, lassi"
drinks = line.split(", ")
drinks.sort()
print(drinks)
print(" | ".join(drinks))Output
['coffee', 'lassi', 'tea']
coffee | lassi | teaTo turn a string of numbers into real numbers, convert each piece.
text = "12 7 30"
numbers = []
for piece in text.split():
numbers.append(int(piece))
print(numbers, sum(numbers))Output
[12, 7, 30] 49Common mistakes
- Writing
marks = marks.sort(). The method returnsNone, so the list is lost. Writemarks.sort()on its own line. - Sorting a list that mixes numbers and text. Python cannot compare the two and stops with a TypeError.
- Expecting a slice to change the original list. It makes a copy.
Try it yourself
Exercise 1. Given prices = [250, 99, 480, 150, 320], print the two cheapest prices.
Show one answer
prices = [250, 99, 480, 150, 320]
print(sorted(prices)[:2])Output
[99, 150]Exercise 2. Print the names in ["meena", "Asha", "ravi"] in alphabetical order, ignoring capital letters.
Show one answer
names = ["meena", "Asha", "ravi"]
print(sorted(names, key=str.lower))Output
['Asha', 'meena', 'ravi']Exercise 3. Two lists hold items and quantities: ["pen", "book", "bag"] and [10, 4, 1]. Print each item with its quantity on one line.
Show one answer
items = ["pen", "book", "bag"]
qty = [10, 4, 1]
for item, q in zip(items, qty):
print(item, q)Output
pen 10
book 4
bag 1Examples on this page were run with Python 3.14 on 12 November 2026. The output shown is the real output.
This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.