01.04 · Create and access lists / Watch · 15 min
Choose an item or a slice
An index selects one item. A slice selects a new list of items.
Activity 4 of 5 in Create and access lists
Python 3: Lists / 01.04
Access items and slices
Not started
Use zero-based and negative indexes, excluded slice stops and nonzero steps.
Your lesson progress
Empty: not started. Yellow: in progress. Green: complete. Marking the whole activity complete does not replace your test results. These circles save in this browser for this pilot.

ORIGINAL WALKTHROUGH · 11:26
Watch with Rob.
Pause whenever you want to try a change. You can return to the lesson at any time.
You are signed in, but video access needs attention. Check your account access. Your saved work is kept.
Video walkthroughs are included with an active Code Zone membership. The written lesson and practice are free.
Open video & code editor (opens in a new tab)Compare the code and its result
Choose an example. The highlighted code and its checked output change together. This demonstration does not change your saved practice.
books = ["Sky", "River", "Forest", "Ocean"]
print(books[0])Sky
Index 0 selects the first item. The result is one string, not another list.
books = ["Sky", "River", "Forest", "Ocean"]
print(books[-1])Ocean
Negative one selects the last item without calculating its positive index.
books = ["Sky", "River", "Forest", "Ocean"]
print(books[1:3])['River', 'Forest']
The slice includes positions 1 and 2, but excludes position 3. Its result is a new list.
The first position is zero
With four items, valid nonnegative indexes are 0 through 3. Negative indexes count backward: -1 selects the last item and -4 selects the first. There is no special last-item index -0; it is just 0.
An out-of-range single index raises IndexError. An empty list has no first or last item.
fruits = ["apple", "banana", "cherry", "date"]
print(fruits[0]) # apple
print(fruits[1]) # banana
print(fruits[-1]) # date
print(fruits[-4]) # appleStart is included; stop is excluded
fruits[1:3] selects indexes 1 and 2. The stop marks where selection ends, not the final selected index. Slicing returns a new, shallow list; it does not remove those items from the original.
Omit the start to begin at zero and omit the stop to continue to the end. Slice bounds beyond the list are clipped instead of raising the index error shown by single-item access.
fruits = ["apple", "banana", "cherry", "date"]
print(fruits[1:3])
print(fruits[:2])
print(fruits[2:])
print(fruits[99:]) # []A step describes the distance between selected indexes
numbers[::2] selects indexes 0, 2 and 4; it does not mean select the items whose values are even. A step of -1 reverses traversal in a new list. A step of zero raises ValueError.
The source asks for three middle numbers from ten values. Ten has no single middle item; this course explicitly chooses indexes 3, 4 and 5, giving 40, 50 and 60.
numbers = [10, 20, 30, 40, 50, 60]
print(numbers[::2]) # [10, 30, 50]
print(numbers[::-1])
print(numbers) # unchangedBuild it one step at a time
Prefer learning by typing? Work through short instructions, run the Python code and check each change before continuing. Your guided project saves separately from the challenge below.
The mini-lesson opens in a new tab. Your own code carries forward; each step has a focused check and an optional hint.
Start mini-lesson(opens in a new tab)5 steps · Saved work · Instant feedback
YOUR TURN
Select list items and slices
Preserve the three input lists. Store and print the four movie endpoints, then middle_numbers [40, 50, 60] and alternate_letters a, c, e, g, i.
Orbit Byte Orbit Byte [40, 50, 60] ['a', 'c', 'e', 'g', 'i']
Keep the starting inputs. Print these lines in this order, without extra output.
- Store Orbit in first_positive
- Store Orbit in first_negative
- Store Byte in last_positive
- Store Byte in last_negative
- Include exactly three middle values
- Start the alternate letters at a
Edit Python, then run it. Check challenge checks your output and the listed Python requirements. Your code saves on this browser after a short pause. Sign in and choose Save account draft to keep it across devices. Word wrap is enabled.
Ready to checkRun the checks to see your results here.
Your check results
Need a nudge?
Count the positions
Five items have positive indexes 0 to 4 and negative indexes -5 to -1.
Stop after the final wanted index
Select indexes 3, 4 and 5 with stop 6. A step of 2 starting at the beginning selects a, c, e, g and i.
One possible solution
Your code may look different and still be correct. Compare the approach with your own.
Each selected value follows the position rule. The slice stop remains excluded, even when it is the last boundary you write.
Walk through the fix.
Choose Next to inspect one change at a time. Highlighted lines show what to notice. Your editor stays unchanged unless you choose to load a step.
Step 1 of 2
Read the inputs
Preserve the three input lists. Store and print the four movie endpoints, then middle_numbers [40, 50, 60] and alternate_letters a, c, e, g, i.
movies = ["Orbit", "Spark", "Loop", "Pixel", "Byte"]
numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]Expected output for this step
(No output)
Build the requested result
Each selected value follows the position rule. The slice stop remains excluded, even when it is the last boundary you write.
movies = ["Orbit", "Spark", "Loop", "Pixel", "Byte"]
numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
first_positive = movies[0]
last_positive = movies[4]
first_negative = movies[-5]
last_negative = movies[-1]
middle_numbers = numbers[3:6]
alternate_letters = letters[::2]
print(first_positive)
print(last_positive)
print(first_negative)
print(last_negative)
print(middle_numbers)
print(alternate_letters)Expected output for this step
Orbit Byte Orbit Byte [40, 50, 60] ['a', 'c', 'e', 'g', 'i']
See the complete solution
movies = ["Orbit", "Spark", "Loop", "Pixel", "Byte"]
numbers = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
letters = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
first_positive = movies[0]
last_positive = movies[4]
first_negative = movies[-5]
last_negative = movies[-1]
middle_numbers = numbers[3:6]
alternate_letters = letters[::2]
print(first_positive)
print(last_positive)
print(first_negative)
print(last_negative)
print(middle_numbers)
print(alternate_letters)Ready to try it?
Practice in your browser and see your results.
Progress details
Have a question?
Ask the Code Zone community.
Ask a question in The Code Zone (opens in a new tab)