thecodezone.
The Code Zone
Short steps, at your pace · Free lessons and practice · Member video walkthroughs
Save your next small win.

Checking your account…

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

All courses

Python 3: Lists / 01.04

Access items and slices

Not started

Use zero-based and negative indexes, excluded slice stops and nonzero steps.

More options

Your lesson progress

Walkthrough: not startedPractice: not started

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.

Rob selects banana at index 1 and date at index -1 in the same four-item fruit list.
Rob’s comic strip · Create and access lists

ORIGINAL WALKTHROUGH · 11:26

Watch with Rob.

Pause whenever you want to try a change. You can return to the lesson at any time.

LEARN WITH THE CODE ZONE

Unlock this video walkthrough

Written lessons and coding practice are free. Video walkthroughs are included with a paid Code Zone membership or an active free trial.

Have a Code Zone website account? Use your learning account’s email to get a six-digit sign-in code, or use your existing website password.

Need a website account? Paid Skool members and active trial members can use the email from their membership questions to request a sign-in code. No invite link or Skool password needed.

Close this window to keep practicing. After signing in, you’ll return to this lesson.

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)

Starter source code for VS Code

Copy the source below or download the file to follow along in VS Code.

The download includes a lesson link in each file and a README so you can find your way back.

main.py
main.pypython · example
# Code along with Rob: Access items and slices
# Compare positive and negative indexes.
# Trace included starts and excluded slice stops.
# Try a step of 2 and a reversed slice; inspect the unchanged original.
# Predict each result, then press play.

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.

example.pypython · example
books = ["Sky", "River", "Forest", "Ocean"]
print(books[0])
PYTHON OUTPUT
Sky

Index 0 selects the first item. The result is one string, not another 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.

The first position is zero
lists.pypython · example
fruits = ["apple", "banana", "cherry", "date"]
print(fruits[0])   # apple
print(fruits[1])   # banana
print(fruits[-1])  # date
print(fruits[-4])  # apple
Open workspace to paste this example (opens in a new tab)

Start 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.

Start is included; stop is excluded
lists.pypython · example
fruits = ["apple", "banana", "cherry", "date"]
print(fruits[1:3])
print(fruits[:2])
print(fruits[2:])
print(fruits[99:])  # []
Open workspace to paste this example (opens in a new tab)

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.

A step describes the distance between selected indexes
lists.pypython · example
numbers = [10, 20, 30, 40, 50, 60]
print(numbers[::2])  # [10, 30, 50]
print(numbers[::-1])
print(numbers)  # unchanged
Open workspace to paste this example (opens in a new tab)

Build 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.

Expected output
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.

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.

Replace your editor code?

Your current work will be replaced. Choose Cancel to keep it.

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)

Continue learning

ONE STEP FORWARD

Trace positions before values. For slices, identify the start, excluded stop and step.