Signup/Sign In
LAST UPDATED: NOVEMBER 10, 2021

Python List: append() and extend() function

    In this tutorial, we will be covering two important library function of Python, namely: append() and extend() function. Both the functions are used to add data elements to the end of the python list but behave differently. Let's see both the functions one by one with examples:

    Python List: append function

    This function adds the element/elements as a single entity to the end of the list. Be it a single element, or a list or a string, it will only add a single entity at the end of the list. In case you want to use this function to add elements in a new list to an existing list, then the new list will be added as a list only.

    Hence, when we use this function, the size of the list increases by only 1 unit.

    It has a constant time complexity of O(1).

    Where do I use append?

    When you wish to insert just one element to a list, the append function can be used.

    Performance:

    When multiple elements are being added, it increases the overhead, because then you will have to call the append() function multiple times and the time complexity of the append function will get close to that of the extend function.

    Time for an Example

    
    element_list = [1,2,5,7,0]
    # appending a new list
    element_list.append([4,9])
    # appending a single element
    element_list.append(10)
    # appending a string
    element_list.append("a string")
    print(element_list)
    
    

    Output:

    [1, 2, 5, 7, 0, [4, 9],10,'a string']


    Python List: extend function

    This function works only with an iterable like list, tuple, etc. If we provide a list or a tuple to it, then this function iterates over the elements of the list and adds every element as a separate entity to the end of the list on which this function is called. Therefore, the size of the original list increases by the same amount as that of the number of elements which are added to the list.

    It has a constant time complexity of O(k) where k is the number of elements which are added to the list.

    Where do I use extend?

    When you wish to append multiple items to a list and the goal is to store them as separate entities/elements, use the extend function.

    Since the extend function has been implemented in C, it would be comparatively quicker than append.

    Time for an Example

    We will have a couple of examples to demonstrate various correct and incorrect ways of using the extend() function.

    Example 1:

    
    element_list = [1,2,5,7,0]
    # appending a new list
    element_list.extend([4,9])
    print(element_list)
    
    

    Output:

    [1, 2, 5, 7, 0, 4, 9]
    

    Example 2:

    
    element_list = [1,2,5,7,0]
    # appending a single element
    element_list.extend(10)
    print(element_list)
    
    

    Output:

    TypeError: 'int' object is not iterable

    Example 3:

    
    element_list = [1,2,5,7,0]
    # appending a tuple
    element_list.extend((10, 11))
    print(element_list)
    
    

    Output:

    [1, 2, 5, 7, 0, 10, 11]


    Conclusion

    Timing the functions shows that the extend() function runs much quicker in comparison to the append(). But the usage of append or extend depends completely on the requirement. As mentioned earlier, if the aim of the developer is to just add one element to a list, append would work great. But if the developer aims to add multiple elements at once, without changing the structure of the list, then extend works much gracefully.

    You may also like:

    I love writing about Python and have more than 5 years of professional experience in Python development. I like sharing about various standard libraries in Python and other Python Modules.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS