An interactive walkthrough of list syntax, indexing, slicing, and modifying values.
Choose a list to explore
List syntax anatomy
Key concepts
Identifier
The variable name that holds the list. Choose something descriptive.
Square brackets [ ]
Always wrap the list contents. An empty [] creates an empty list.
Elements
Any values separated by commas. Order is preserved.
Index
Each element has a position number starting at 0. Use list[i] to access it.
More examples
Empty list
empty= []
Mixed types are allowed
mixed= [42,"hello",3.14,True]
Indexing — access one element
Each element has a position number called an index. Python starts counting at 0. You can also count backwards using negative indices (−1 is the last item).
Try it — click any cell or type an index below
Index:
Index cheat sheet
Positive index
0 = first, 1 = second, etc. Counts from the front.
Negative index
−1 = last, −2 = second to last. Counts from the back.
Valid range
Must be between −len(list) and len(list)−1, otherwise you get an IndexError.
Syntax
list_name[index]
Slicing — access a range of elements
A slice returns a new list containing elements from start up to (but not including) stop. The optional step skips elements.
Try it — adjust start, stop, and step
Start:Stop:Step:
The stop index is exclusive — the element at stop is never included.
Slice shorthand
Omit start — defaults to 0
fruits[:3] # first 3 elements
Omit stop — goes to end
fruits[2:] # from index 2 to end
Use a step — every Nth element
fruits[0:5:2] # every other element
Copy the whole list
copy=fruits[:]
Reassigning a value
You can change any element by using its index on the left side of =. The rest of the list stays the same.
list_name[index]=new_value
Try it — pick an index and a new value
Index:New value:
!Reassignment replaces the element at that index. It does not insert a new one or shift other elements.
Printing specific elements
Use print(list_name[index]) to display a single element. Click an index chip below to see the output.
Try it — click an index to print that element
You can also print the whole list by just using the identifier: print(fruits)