Last modified: Aug 12, 2026

Python List Size: 5 Easy Examples

Knowing the size of a Python list is a fundamental skill. You often need to check how many items are in a list. This helps with loops, conditionals, and data analysis. In this guide, we will explore different ways to get the size of a list. You will learn the difference between logical length and memory size.

We will use clear examples. Each example will show the code and the output. This makes it easy to follow along. Let's dive into the world of Python list sizes.

Using the len() Function

The most common way to find the size of a list is using the len() function. This function returns the number of items in the list. It is fast, simple, and built into Python. You should use this for almost all your needs.

Here is a basic example. We create a list with a few elements. Then we pass it to len() to get its size.


# Create a list of fruits
fruits = ['apple', 'banana', 'cherry']

# Get the size of the list
size = len(fruits)

# Print the result
print("The list has", size, "items.")

The list has 3 items.

This is the standard approach. It works for any iterable, not just lists. You can use it on strings, tuples, and dictionaries. The len() function is your primary tool for finding the logical size of a list.

Checking an Empty List

Sometimes you need to check if a list is empty. You can do this by comparing the size to zero. A list with a size of zero has no elements. This is a common pattern in programming.

Let's see how to check for an empty list. We will use the len() function in an if statement. This helps you avoid errors when processing data.


# Create an empty list
my_list = []

# Check if the list is empty
if len(my_list) == 0:
    print("The list is empty.")
else:
    print("The list has items.")

The list is empty.

This method is clear and readable. It is often used before iterating over a list. For a deeper look at conditional checks, you can see our guide on Python List Length Check with If. This will give you more patterns for using size in conditions.

Memory Size with sys.getsizeof()

The len() function gives you the number of items. But what about the memory size? Each list takes up space in your computer's RAM. To see this, you can use the sys.getsizeof() function. This returns the size of the object in bytes.

This is useful for optimizing memory. It shows you how much space a list consumes. The size includes the list object itself and the pointers to its elements. It does not include the size of the elements themselves.


import sys

# Create a list with numbers
numbers = [10, 20, 30, 40, 50]

# Get the memory size in bytes
memory_size = sys.getsizeof(numbers)

# Print the memory size
print("Memory size of the list:", memory_size, "bytes")

Memory size of the list: 104 bytes

Notice that the memory size is larger than the number of items. This is because lists store extra information. The exact number may vary by system. But this gives you a good idea of the overhead.

Memory size is different from logical size. Logical size is the count of items. Memory size is the storage footprint. Remember this distinction when working with large datasets.

Size of a Nested List

Nested lists are lists inside lists. The len() function only counts the top-level items. It does not count the items inside the inner lists. This is important to understand to avoid confusion.

Let's examine a nested list. We will see how len() behaves with it. The outer list has three elements. Each element is itself a list.


# Create a nested list
matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

# Get the size of the outer list
outer_size = len(matrix)

# Print the size
print("Number of rows:", outer_size)

# Get the size of the first inner list
inner_size = len(matrix[0])
print("Number of columns:", inner_size)

Number of rows: 3
Number of columns: 3

This example shows a 3x3 matrix. The outer list has 3 rows. Each row is a list with 3 columns. You must call len() on each inner list to get its size. This is a common task in data processing.

Performance and Large Lists

The len() function is very fast. It runs in constant time, O(1). This means it takes the same time regardless of the list size. Python stores the length of the list internally. So len() just reads that value.

This makes it perfect for large lists. You can call it millions of times without worry. It is one of the most efficient operations in Python. Here is an example with a large list.


# Create a large list using range
large_list = list(range(1000000))

# Get the size quickly
size = len(large_list)

# Print the size
print("Size of large list:", size)

Size of large list: 1000000

This code runs instantly. Even with a million items, len() is immediate. This is a key advantage of Python lists. You can rely on this performance in your applications.

When you are done with a list, you might want to remove items. You can use the del statement to remove items by index. Check out our guide on Python List del: Remove Items Easily for more details. This helps you manage the size of your lists dynamically.

Adding Items and Size Changes

Lists are mutable. This means you can change their size after creation. You can add items to the end. Each time you add an item, the size increases by one. The len() function reflects this change immediately.

Let's see how size changes when adding items. We will use the append() method. This adds an element to the end of the list.


# Start with an empty list
cart = []

# Check initial size
print("Initial size:", len(cart))

# Add items
cart.append('book')
print("Size after 1 item:", len(cart))

cart.append('pen')
print("Size after 2 items:", len(cart))

Initial size: 0
Size after 1 item: 1
Size after 2 items: 2

This is straightforward. The size always matches the number of items you have added. For more ways to add items, see our article on Python List Append to End. It covers all the methods for growing your list.

This dynamic behavior is powerful. You can build lists of any length. The size updates automatically. This makes lists perfect for collections that change over time.

Conclusion

Finding the size of a Python list is simple. The len() function is your main tool. It gives you the number of items quickly. Use it for loops, conditions, and data checks.

For memory size, use sys.getsizeof(). This shows the storage footprint in bytes. Remember that this includes overhead. It is different from the item count.

We covered empty lists and nested lists. We also discussed performance. The len() function is always fast. You can use it on lists of any size without delay.

Lists are dynamic. Their size changes as you add or remove items. This flexibility is a core feature of Python. Now you know all the ways to check the size of a list. Use these examples to write better code.