Lists

Python Lists

Lists are ordered, mutable collections that can hold items of any type. They're one of Python's most versatile data structures.
python
fruits = ["apple", "banana", "cherry"]
numbers = [1, 2, 3, 4, 5]
mixed = [1, "hello", True, 3.14]

print(fruits[0])   # apple (first item)
print(fruits[-1])  # cherry (last item)

List Methods

Lists come with powerful built-in methods for adding, removing, and manipulating items.
python
colors = ["red", "green"]
colors.append("blue")       # Add to end
colors.insert(1, "yellow")  # Insert at position
print(colors)

colors.remove("green")      # Remove by value
last = colors.pop()         # Remove and return last
print(colors)
print(f"Removed: {last}")

List Comprehensions

List comprehensions are a concise way to create lists. They're a signature Python feature.
python
squares = [x ** 2 for x in range(6)]
print(squares)  # [0, 1, 4, 9, 16, 25]

evens = [x for x in range(10) if x % 2 == 0]
print(evens)  # [0, 2, 4, 6, 8]

Tip:Lists are mutable — you can change, add, and remove items after creation. Use tuples (1, 2, 3) when you need an immutable sequence.

PlaygroundPython
Output
Click "Run" to see output

💬 Got questions? Ask me anything!