Tuples vs Lists in Python

flat screen computer monitor displaying white and black screen

In short, the main difference between tuples and lists in Python is mutability. A Python list can be changed after creation, but a tuple cannot. This means a tuple performs slightly better than a list. But a tuple has less use cases, as one cannot change its contents.

In short, here are all the key differences between tuples and lists.

ListTuple
  • Mutable
  • Immutable
  • Higher memory consumption
  • Lower memory consumption
  • A lot of built-in methods are available
  • A small number of built-in methods available
  • Slower iteration
  • Faster iteration
  • Better for operations like insertion and deletion
  • Better for accessing elements
  • Notice how all these differences are a consequence of a tuple being immutable.

    Let’s go through the differences in a bit more detail. But before we do that, let’s briefly describe what are tuples and lists in the first place.

    What Is a Tuple in Python

    A tuple is a commonly used data type in Python. You can create tuples by comma-separating items between parenthesis.

    For example:

    names = ("James", "Adam", "Paul")

    After creating a tuple, you cannot make changes to it. A tuple is meant to hold values that are not supposed to change over time.

    What Is a List in Python

    A list is a popular data type in Python. Lists store multiple items that can be accessed, modified, and removed easily.

    To create a list, add comma-separated items between a set of square brackets.

    For example:

    names = ["James", "Adam", "Paul"]

    Let’s next start making comparisons between lists and tuples.

    Tuples vs Lists in Python—Detailed Comparison

    1. Syntax Differences

    The most obvious difference between a tuple and a list is the syntax for creating them.

    While a list is created using square brackets, a tuple is created using regular parenthesis.

    For example:

    num_list  = [1, 2, 3, 4, 5]
    num_tuple = (1, 2, 3, 4, 5)

    2. Mutability

    A List Is Mutable

    A list is mutable. In other words, you can change its elements after creating it. You can add, remove, or update elements of the list.

    numbers = [1, 2, 3]
    
    # Add a new element to the end of a list.
    numbers.append(4)
    
    # Remove the first element of a list.
    numbers.pop(0)
    
    # Update the 3rd element of a list.
    numbers[2] = 1000
    
    print(numbers)

    Tuple Is Immutable

    A tuple is immutable. This means you cannot change its contents after it has been created.

    For example, let’s try to update the first element of a tuple:

    numbers = (1, 2, 3)
    
    # TRYING to update a value of a tuple FAILS
    numbers[0] = 1000

    This results in an error:

    TypeError: 'tuple' object does not support item assignment

    If you want to add, remove, or update a value in a tuple, you need to create a new tuple. But you cannot directly modify a tuple in any way.

    3. Memory and Performance

    Tuples consume a bit less memory than lists.

    For example, let’s compare two identical groups of items where the other one is a tuple and the other is a list.

    names_tuple = ('Nick', 'Jan', 'Sophie')
    names_list = ['Nick', 'Jan', 'Sophie']
    
    print(names_tuple.__sizeof__())
    print(names_list.__sizeof__())

    Output:

    48
    104

    The result of calling the __sizeof__() method is the internal size of the object in bytes. Here you can see that the tuple takes only 48 bytes, whereas the list requires 104.

    As a tuple cannot be modified, its size remains the same. Thus, a tuple does not have to over-allocate space. In contrast, a list mildly over-allocates space to make the append() method work efficiently.

    4. Built-In Methods

    A tuple has fewer built-in methods than a list. This is mainly because a tuple is immutable. Thus operations such as insertion, deletion, or update would be meaningless.

    Here is a bunch of useful methods for updating a list (that you already saw):

    numbers = [1, 2, 3]
    
    numbers.append(4)
    numbers.pop(0)
    numbers[2] = 1000

    None of these built-in list methods work for tuples.

    To get a list of all the available methods of tuples and lists, use the dir() function:

    dir(tuple)
    dir(list)

    The dir() function returns a big list that consists of all the built-in methods for the type. At the time of writing, list has 47 built-in methods, whereas a tuple only has 34.

    Should I Use a Tuple or a List

    Now that you know the physical differences between tuples and lists in Python, it’s good to finish off by learning which one to pick.

    Most of the time, you benefit from lists more than tuples.

    But a tuple is useful when you want to create a collection of elements that should not change over time. Using a tuple in this case is good, as it makes it harder for you to make mistakes. This is because changing a tuple value is not possible, as tuples are immutable.

    Conclusion

    The key difference between a list and a tuple is mutability. A Python list can be changed after creation. But a tuple cannot.

    Thanks for reading. I hope you find it useful.

    Happy coding!

    Further Reading

    50 Python Interview Questions

    Share on facebook
    Facebook
    Share on google
    Google+
    Share on twitter
    Twitter
    Share on linkedin
    LinkedIn
    Share on pinterest
    Pinterest

    Leave a Comment

    Your email address will not be published. Required fields are marked *