Python Lists: Create, Access and Modify Them

    Infoplanet2 min readUpdated

    A list is an ordered, changeable collection of items. It is the data structure you will use most often in Python, so it is worth getting comfortable with the basics early.

    Creating a list

    You write a list with square brackets and commas between the items. A list can hold numbers, strings, or a mix of types:

    fruits = ["apple", "banana", "cherry"]
    numbers = [10, 20, 30, 40]
    mixed = ["text", 42, True]
    empty = []
    

    Accessing items by index

    Each item has a position, called its index, starting at 0. You read an item by putting its index in square brackets:

    fruits = ["apple", "banana", "cherry"]
    print(fruits[0])   # apple
    print(fruits[2])   # cherry
    print(fruits[-1])  # cherry  (negative counts from the end)
    

    Negative indexes count back from the end, so -1 is the last item — handy when you do not know the list length.

    Slicing a range

    A slice gives you part of a list. The syntax is list[start:stop], where stop is not included:

    numbers = [10, 20, 30, 40, 50]
    print(numbers[1:3])  # [20, 30]
    print(numbers[:2])   # [10, 20]
    print(numbers[3:])   # [40, 50]
    

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    Adding and removing items

    Lists are mutable, which means you can change them after they are created:

    fruits = ["apple", "banana"]
    fruits.append("cherry")      # add to the end
    fruits.insert(1, "mango")    # add at index 1
    fruits.remove("banana")      # remove by value
    last = fruits.pop()          # remove and return the last item
    print(fruits)
    

    append adds a single item to the end, insert places one at a specific position, remove deletes the first matching value, and pop removes and returns an item (the last one by default).

    Looping through a list

    A for loop is the natural way to visit every item:

    fruits = ["apple", "banana", "cherry"]
    for fruit in fruits:
        print(fruit)
    

    If you also need each item's index, enumerate gives you both:

    for index, fruit in enumerate(fruits):
        print(index, fruit)
    

    A few things to remember

    • Indexing starts at 0, so the third item is at index 2.
    • A slice's stop value is excluded, so numbers[1:3] returns two items.
    • Lists can grow and shrink, unlike tuples, which are fixed once created.

    Once lists feel natural, dictionaries and sets are easy next steps — they share the same indexing and looping ideas with a few twists. Try building a small shopping list program that adds, removes and prints items to lock in what you have learned here.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    Infoplanet

    Coding, AI & Robotics

    Infoplanet is a coding, AI and robotics education center teaching students of all ages since 2001.

    Related guides