4.2.3 Slicing Lists

Unit: Unit 4: Strings, Lists, and Tuples • Interactive Practice

Quick Answer & Summary:

Interactive Python programming tutorial and practice in your browser.

Interactive Challenge (Run Live in Browser):

# Slice sub-lists from the inventory
inventory = ["laptop", "mouse", "keyboard", "monitor", "headphones"]

# 1. Slice the first 3 items (indices 0, 1, 2)
core_peripherals = inventory[0:3]

# 2. Slice from index 2 to the end
remaining_items = inventory[2:]

# 3. Create a shallow copy of the entire list using [:]
inventory_copy = inventory[:]

print("Core peripherals:", core_peripherals)
print("Remaining items:", remaining_items)
print("Is identical object:", inventory is inventory_copy)
print("Has identical values:", inventory == inventory_copy)
Open in Live Interactive Python Sandbox →