Data Structures

Lists

A list is an ordered, changeable collection of items.

Creating Lists

fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", True, 3.14]

Accessing Items

fruits = ["apple", "banana", "cherry"]

fruits[0]   # "apple" (first item)
fruits[-1]  # "cherry" (last item)
fruits[1:3] # ["banana", "cherry"] (slice)

Modifying Lists

fruits = ["apple", "banana"]

fruits.append("cherry")     # Add to end
fruits.insert(0, "mango")   # Insert at position
fruits.remove("banana")     # Remove by value
fruits.pop()                # Remove last
fruits[0] = "grape"         # Change item

List Length

len(fruits)  # Number of items
Visualizer
$ python lists.py
First: apple Last: cherry After append: ['apple', 'banana', 'cherry', 'mango'] - apple - banana - cherry - mango

Interactive Visualization

python
Loading...
Output
Run execution to see output...