Getting Elements Out of a List
The problem...
You have a list. Five names, ten scores, a hundred values.
But you don't always need the whole thing.
You need one element. Or a few. Or everything from position three onwards.
How do you reach in and get exactly what you want?
The idea!
Every element in a list has a position — a number that tells Python exactly where it sits.
That number is called an index.
You use it to pull out one element at a time.
And when you need a chunk of the list, you use a slice.
Indexing
Square brackets. Index number inside. That's it.
squad = ["Raven", "Wolf", "Ghost", "Viper", "Bull"]
print(squad[0])
Output → Raven
Python counts from zero. The first element is always at index 0.
The full picture
squad = ["Raven", "Wolf", "Ghost", "Viper", "Bull"]
# 0 1 2 3 4
squad[0]→Ravensquad[1]→Wolfsquad[2]→Ghostsquad[3]→Vipersquad[4]→Bull
Negative indexes
Python also lets you count from the end. Negative indexes go backwards.
squad = ["Raven", "Wolf", "Ghost", "Viper", "Bull"]
print(squad[-1])
Output → Bull
-1 is always the last element. -2 is the one before that.
# -5 -4 -3 -2 -1
squad = ["Raven", "Wolf", "Ghost", "Viper", "Bull"]
Useful when you need the last element and don't know the length of the list.
Slicing
A slice gives you a portion of the list — a new list made from a range of elements.
The syntax is list[start:stop]. Start is included. Stop is not.
squad = ["Raven", "Wolf", "Ghost", "Viper", "Bull"]
print(squad[1:4])
Output → ['Wolf', 'Ghost', 'Viper']
From index 1 up to — but not including — index 4.
Leaving start or stop empty
If you leave start empty, Python starts from the beginning.
If you leave stop empty, Python goes to the end.
squad = ["Raven", "Wolf", "Ghost", "Viper", "Bull"]
print(squad[:3]) # first three
print(squad[2:]) # from index 2 to the end
print(squad[:]) # full copy
Output:
['Raven', 'Wolf', 'Ghost']
['Ghost', 'Viper', 'Bull']
['Raven', 'Wolf', 'Ghost', 'Viper', 'Bull']
Slice with a step
A third number controls the step — how many positions to jump each time.
squad = ["Raven", "Wolf", "Ghost", "Viper", "Bull"]
print(squad[::2])
Output → ['Raven', 'Ghost', 'Bull']
Every second element. Step defaults to 1 when you leave it out.
A step of -1 reverses the list:
print(squad[::-1])
Output → ['Bull', 'Viper', 'Ghost', 'Wolf', 'Raven']
What's really happening
Indexing returns a single element — the value itself.
Slicing returns a new list — a copy of the portion you asked for.
The original list is never changed by either operation.
Heads up!
- Indexes start at
0, not1 - Using an index that doesn't exist raises an
IndexError - Slice stop is exclusive —
list[1:4]gives you indexes 1, 2, 3 - Slicing never raises an error even if the range is out of bounds — it just returns what it can
list[-1]is the last element regardless of list length
The mindset shift
Stop thinking: "I have a list."
Start thinking: "I have a list, and I can reach any part of it — one element or many."
What you should understand now
- Use
list[index]to get a single element - Indexes start at
0— negative indexes count from the end - Use
list[start:stop]to get a range of elements - Leave
startorstopempty to go from the beginning or to the end - Add a step with
list[start:stop:step] - Slicing returns a new list — the original is unchanged