Live
Auto strategy optimizer — AI improves your edge while you sleep
guidebeginner18 min

Lists: Ordered Collections

Store multiple items in a single variable. Learn to create, access, and manipulate lists.

Last updated: Jan 28, 2026

Lists let you store multiple items in a single variable. They're ordered (items stay in the order you put them) and changeable (you can add, remove, or modify items).

Creating Lists

python
# List of numbers
numbers = [1, 2, 3, 4, 5]

# List of strings
fruits = ["apple", "banana", "cherry"]

# Mixed types (allowed, but usually avoid)
mixed = [1, "hello", 3.14, True]

# Empty list
empty = []

print(fruits)  # ['apple', 'banana', 'cherry']

Accessing Items (Indexing)

Items are accessed by their position (index), starting at 0:

python
fruits = ["apple", "banana", "cherry", "date"]
#            0         1          2        3
#           -4        -3         -2       -1  (negative indices)

print(fruits[0])   # apple (first item)
print(fruits[2])   # cherry (third item)
print(fruits[-1])  # date (last item)
print(fruits[-2])  # cherry (second to last)

Slicing: Getting Multiple Items

python
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# [start:stop] - from start to stop-1
print(numbers[2:5])   # [2, 3, 4]

# [:stop] - from beginning to stop-1
print(numbers[:4])    # [0, 1, 2, 3]

# [start:] - from start to end
print(numbers[6:])    # [6, 7, 8, 9]

# [start:stop:step] - with step
print(numbers[::2])   # [0, 2, 4, 6, 8] (every other)
print(numbers[::-1])  # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] (reversed)

Modifying Lists

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

# Change an item
fruits[1] = "blueberry"
print(fruits)  # ['apple', 'blueberry', 'cherry']

# Add items
fruits.append("date")        # Add to end
print(fruits)  # ['apple', 'blueberry', 'cherry', 'date']

fruits.insert(1, "banana")   # Insert at position
print(fruits)  # ['apple', 'banana', 'blueberry', 'cherry', 'date']

# Remove items
fruits.remove("cherry")      # Remove by value
print(fruits)  # ['apple', 'banana', 'blueberry', 'date']

last = fruits.pop()          # Remove and return last item
print(last)    # date
print(fruits)  # ['apple', 'banana', 'blueberry']

del fruits[0]                # Delete by index
print(fruits)  # ['banana', 'blueberry']

List Operations

python
# Length
numbers = [1, 2, 3, 4, 5]
print(len(numbers))  # 5

# Check if item exists
print(3 in numbers)   # True
print(10 in numbers)  # False

# Combine lists
a = [1, 2, 3]
b = [4, 5, 6]
c = a + b
print(c)  # [1, 2, 3, 4, 5, 6]

# Repeat list
print([1, 2] * 3)  # [1, 2, 1, 2, 1, 2]

# Sort
numbers = [3, 1, 4, 1, 5, 9, 2, 6]
numbers.sort()
print(numbers)  # [1, 1, 2, 3, 4, 5, 6, 9]

# Reverse
numbers.reverse()
print(numbers)  # [9, 6, 5, 4, 3, 2, 1, 1]

Useful List Functions

python
numbers = [3, 1, 4, 1, 5, 9]

print(min(numbers))     # 1 (smallest)
print(max(numbers))     # 9 (largest)
print(sum(numbers))     # 23 (total)
print(numbers.count(1)) # 2 (how many 1s)
print(numbers.index(4)) # 2 (position of 4)

Looping Over Lists

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

# Simple loop
for fruit in fruits:
    print(fruit)

# With index
for i, fruit in enumerate(fruits):
    print(f"{i}: {fruit}")
# 0: apple
# 1: banana
# 2: cherry

List Comprehensions

A concise way to create lists:

python
# Create a list of squares
squares = [x ** 2 for x in range(5)]
print(squares)  # [0, 1, 4, 9, 16]

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

# Transform items
fruits = ["apple", "banana", "cherry"]
upper_fruits = [f.upper() for f in fruits]
print(upper_fruits)  # ['APPLE', 'BANANA', 'CHERRY']

Practice

  1. Create a list of your 5 favorite movies and print the first and last
  2. Write a function that takes a list and returns it reversed (without using .reverse())
  3. Remove all duplicates from a list
  4. Find the second largest number in a list
  5. Use list comprehension to get all words longer than 4 characters from a list of words

Tags

listsarrayscollectionsindexingslicing
Related documentation