Start learning free

LESSON 96 OF 410 · LISTS I

Lists in Python

Lesson Read the full explanation

When we're dealing with large amounts of data, we want to make sure we can organize and manage it properly.

In Python, we use lists to store several pieces of data in one place.


List Declaration / Creation:

You start a list declaration with an opening square bracket, end it with a closing square bracket, and put a comma between each entry, like this:

sandwich = ["peanut butter", "jelly", "bread"]

In this program we created a list consisting of 3 string items: "peanut butter", "jelly" and "bread".

Then, we stored that list in a variable sandwich so we can later access it. For example, I can print the list:

# Creating the list:
sandwich = ["peanut butter", "jelly", "bread"]

# Printing the list:
print(sandwich) # Output: ["peanut butter", "jelly", "bread"]

Items Data Types

Lists can store any data types (strings, integers, booleans, etc.) and the items shouldn't necessary be of the same data type. For example:

myList = ["Hello world!", 32, False]

myList consits of one string item, one number item and one boolean item.

We use lists to better organize pieces of data which are part of the same family or share the same meaning, like the name of our friends or the items in a sandwich.

So most of the time we will have lists where the items share the same data type.


Assignment
Follow the Coding Tutorial and let's play with some arrays.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    PYTHON · LISTS I

    Arrays in Python, explained

    Python Lists: The Go-To Data Structure for Collections

    Almost every real program deals with groups of things — a shopping cart full of items, a leaderboard of player scores, a to-do list of tasks. Python's list is the built-in tool for storing ordered collections of data. Once you understand lists, you can write programs that handle real-world data at any scale.

    Lists are mutable (you can add, remove, and change items), ordered (items stay in the order you put them), and flexible (they can hold any mix of data types). They're used in everything from web APIs to machine learning datasets.

    Creating a Python List

    Wrap your items in square brackets [], separated by commas. That's it:

    # A list of strings
    cities = ["New York", "London", "Tokyo"]
    
    # A list of numbers
    scores = [98, 74, 85, 91]
    
    # A mixed list (any types work)
    profile = ["Alex", 28, True, 4.9]
    
    # An empty list (to fill later)
    cart = []
    
    print(cities)   # ['New York', 'London', 'Tokyo']
    print(scores)   # [98, 74, 85, 91]
    

    Accessing Items by Index

    Python lists are zero-indexed — the first item is at position 0, the second at 1, and so on. Use square bracket notation to access any item:

    cities = ["New York", "London", "Tokyo"]
    
    print(cities[0])   # New York  (first item)
    print(cities[1])   # London    (second item)
    print(cities[2])   # Tokyo     (third item)
    
    # Negative indexing counts from the end:
    print(cities[-1])  # Tokyo     (last item)
    print(cities[-2])  # London    (second to last)
    

    Modifying a List

    Unlike strings, lists are mutable — you can change, add, and remove items after creation:

    scores = [98, 74, 85]
    
    # Change an item:
    scores[1] = 80
    print(scores)   # [98, 80, 85]
    
    # Add to the end:
    scores.append(91)
    print(scores)   # [98, 80, 85, 91]
    
    # Remove by value:
    scores.remove(80)
    print(scores)   # [98, 85, 91]
    
    # Remove by index:
    scores.pop(0)
    print(scores)   # [85, 91]
    

    Real-World Example: Managing a Task List

    tasks = ["Write report", "Send email", "Review code"]
    
    # Mark task done by removing it:
    tasks.remove("Send email")
    
    # Add a new task:
    tasks.append("Update docs")
    
    # How many tasks remain?
    print("Tasks left:", len(tasks))   # Tasks left: 3
    print(tasks)
    # ['Write report', 'Review code', 'Update docs']
    

    Useful List Operations at a Glance

    • len(my_list) — number of items in the list
    • my_list.append(x) — add x to the end
    • my_list.insert(i, x) — insert x at index i
    • my_list.remove(x) — remove the first occurrence of x
    • my_list.pop(i) — remove and return item at index i
    • x in my_list — check if x exists in the list (returns True/False)
    • my_list.sort() — sort the list in place

    Common Mistakes to Avoid

    • IndexError — accessing out-of-range positions: A list with 3 items only has indices 0, 1, and 2. Trying list[3] crashes.
    • Confusing append() with extend(): append([1, 2]) adds one nested list item; extend([1, 2]) adds two separate items.
    • Modifying a list while iterating over it: This can skip items or cause errors. Iterate over a copy instead.
    nums = [1, 2, 3]
    
    # IndexError:
    # print(nums[5])  # IndexError: list index out of range
    
    # append vs extend:
    a = [1, 2]
    a.append([3, 4])
    print(a)   # [1, 2, [3, 4]]  — one nested list
    
    b = [1, 2]
    b.extend([3, 4])
    print(b)   # [1, 2, 3, 4]   — four separate items
    

    Key Takeaways

    • A Python list stores an ordered collection of items in square brackets: my_list = [a, b, c].
    • Access items with their index: my_list[0] for the first, my_list[-1] for the last.
    • Lists are mutable — use append(), remove(), and pop() to modify them.
    • Use len() to check how many items a list contains.

    What to Learn Next

    With lists in your toolkit, the next step is learning how to access individual elements by index and how to loop through a list with a for loop. These two skills let you process every item in a collection — the basis of data processing in Python.

    Keep going with PythonAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free