Module 03 · Lesson 01
Lists & Indexing Operations
Keep many values in order with lists: index and slice them, change them in place with append, insert, remove and pop, and sort them with sort() or sorted().
- About 8 minutes
- Beginner friendly
- 12-question quiz at the end
What you will learn
- Make and index lists
- Slice a piece
- Add, remove and sort
- Watch a queue move
Many values, one name
A list keeps values in order under one name, and you can change it whenever you like.
Make one
Square brackets and commas:
pets = ["cat", "dog", "fish"].Index
pets[0]is the first item andpets[-1]the last. Counting starts at 0.Change it
append()adds to the end,insert()adds anywhere,remove()andpop()take items out.Look inside
len()counts the items,inchecks for one,sorted()gives them in order.
Lists everywhere
Any time an app shows things in a row, there is probably a list behind it.
- Up nextA music queue is a list: play
[0], then take it off the front. - High scoresAdd the new score, sort the list, keep the top ten with a slice.
- Shopping listsAdd items as you think of them and remove them as they go in the basket.
- ChatsEach new message is appended to the end of the conversation.
Calling a list method
A method is a command that belongs to a value. Tap each part to see its job.
Part 1 of 4
list:The list you want to change. It must already exist, for example team = ["Ana"].
Build and change lists
Pick an example, change it, then press Run. Say what you expect to see before you press.
Example 1 of 6Make a list
Index 0 is the first item, -1 the last.
Your turn: Print the second pet using a positive index, then using a negative one.
pets = ["cat", "dog", "fish", "rabbit"]
print(pets)
print(len(pets), "pets")
print("First:", pets[0])
print("Last:", pets[-1])Press Run to see what it prints.Watch a queue move
Press Step to run one line at a time. Watch the list in the names box grow and shrink.
Ready. Press Step to run the first line.
- queue = ["Ana", "Ben"]
- queue.append("Cy")
- first = queue.pop(0)
- queue.insert(0, "Dee")
- print(first, "is served")
- print(queue)
Output so far
Nothing printed yet.
What Python does
Python reads the program from the top. Nothing has run yet.
Names
None yet.
Quick guesses
Guess first, then read why. A wrong guess is where the learning happens. Nothing here is scored.
Question 1 of 6
1.Counting from the end: what prints?
nums = [10, 20, 30]
print(nums[-2])0 of 6 answered
Common list misconceptions
Each card is something beginners often believe. Tap it to see what Python really does.
Misconception 1 of 4
Notes
Everything above, in words. Open it when you want the full explanation.
Read the notesNotesOpenClose
Syntax
The general shape of the statements in this lesson.
items = [1, 2, 3] len(items) items[0] items[-1] # first, last items[1:3] items[::-1] # slice, reversed copy items.append(x) items.insert(i, x) items.extend(more) items.remove(x) items.pop() items.pop(i) x in items items.index(x) items.count(x) items.sort() # in place, returns None sorted(items) # new list; reverse=True, key=len copy = items.copy()
Press Run to see what it prints.Worked examples
Read the problem, run the code, then read why it works.
Example 1 of 2
A top three leaderboard
Given a list of scores, print the top three from highest to lowest without changing the original list, then show the original is untouched.
scores = [56, 91, 73, 88, 64, 91]
top = sorted(scores, reverse=True)[:3]
print("Top three:", top)
print("Original:", scores)
print("Best score appears", scores.count(91), "times")Press Run to see what it prints.Predict first
How it works(open after your guess)
Common pitfalls
Mistakes people make with this topic, and what to write instead.
Pitfall 1 of 3
Assigning the result of a method
Spot the bug
Can you see what is wrong? Run it to watch it go wrong, then open the fix.
numbers = [3, 1, 2] numbers = numbers.sort() print(numbers)
Press Run to see what it prints.Show what goes wrong and the fixWhat goes wrong and the fix
In the real world
Where this shows up in software people use every day.
- Education
A class reading log
A teacher keeps the minutes each pupil read this week in a list, then reports the average, the range and the three longest sessions without losing the register order.
Why Python fits
Built-in functions answer the summary questions in one line each, and
sorted()produces a ranking while the list itself stays in the same order as the class register.Pythonminutes = [25, 40, 15, 60, 30, 45] average = sum(minutes) / len(minutes) print(f"Average: {average:.1f} min") print(f"Shortest {min(minutes)}, longest {max(minutes)}") print("Longest three:", sorted(minutes, reverse=True)[:3]) print("Register order kept:", minutes)OutputPress Run to see what it prints.
Key takeaways
Tick each one you could explain to a friend.
0 of 5
Common questions
Guess the answer first, then open it.
Can a list hold different types?
What is the difference between append and extend?
How do I sort names ignoring capital letters?
Your notes
Sign in to keep private notes alongside this lesson.
Where to go next
The quiz and the Lab live on their own pages, so this lesson stays about learning.
Finished this lesson?
Create an ID or sign in to save your progress across devices.
Check what you learned
12 questions, with an explanation after every answer. A verified attempt counts towards opening the module Lab.
Keep going in the Lab
7 tasks on this lesson, from writing code to tracing it by hand. Practice only, no XP.
Spaced review
Questions you miss in a quiz come back on a spaced schedule, so what you learned here sticks.
This lesson at a glance
The 9 lines of code this lesson uses, each with a short note. Keep it open while you do the quiz and the Lab.
What else you can search for and explore
Curious to go further? Each card opens a web search in a new tab.
References
Look things up here whenever you need the exact details.