You are currently viewing Python Difference between List append() vs extend()

What is the difference between list append() vs extend() methods in Python? The append() and extend() methods are two commonly used methods to add elements to a List. In Python, List is the most commonly used data type. We can perform many operations on lists. One of the most frequent operations on lists is adding elements.

Advertisements

While both methods add elements to a list, they work in different ways and have important differences that affect the behavior of the list. In this article, we will explore the differences between append() and extend(), and provide examples.

1. Difference Between append() and extend()

The append() and extend() are both list methods in Python that add elements to the end of a list. However, they work in different ways and have different characteristics that make them appropriate for different use cases.

The following table will give you quick glance at the difference between the append() and extend()methods in Python.

Featureappend()extend()
Argument typeTakes a single objectTakes an iterable (e.g., list, tuple, string)
Nested listsAdds nested list as a single elementAdds elements of nested list separately
Number of elements addedAdds one element at a timeCan add any number of elements at once
PerformanceFaster for adding a single elementFaster for adding multiple elements at once
append() vs extend()

2. Difference in Syntaxes

The Syntax of append():


# Syntax of append()
list.append(element)

The Syntax of extend():


# Syntax of extend()
list.extend(iterable)

The below sections cover the main differences between the append() and extend() methods along with examples.

3. append() vs extend() – Argument type

The append() method takes a single argument, which is the element that you want to add to the end of the list. This argument can be of any data type, such as a string, integer, or another list.

When you try to append a list, it will add the entire list as a single element creating a nested list.


# Append single element
my_list = [1, 2, 3]
my_list.append(4)

# Output:
# [1, 2, 3, 4]

On the other hand, the extend() method takes an iterable as its argument. An iterable is any object that can be looped over, such as a list, tuple, or string.


# Append multiple elements
my_list.extend([4, 5, 6])

# Output: 
# [1, 2, 3, 4, 5, 6]

The below example will give you a more clear idea of the differences:

Example 1: Adding a single string


# Using append()
my_list = ['Python', 'Java', 'C++']
my_list.append('Ruby')
print(my_list)

# Output: 
# ['Python', 'Java', 'C++', 'Ruby']

# Using extend()
my_list.extend('JavaScript')
print(my_list)

# Output: 
# ['Python', 'Java', 'C++', 'R', 'u', 'b', 'y']

Example 2: Example 2: Adding a single tuple


# Append tuple to list
my_list.append(('JavaScript', 'Ruby'))
print(my_list)

# Output: 
# ['Python', 'Java', 'C++', ('JavaScript', 'Ruby')]

# Using extend() with tuple
my_list.extend(('JavaScript', 'Ruby'))
print(my_list)

# Output:
# ['Python', 'Java', 'C++', 'JavaScript', 'Ruby']

4. Nested lists with extend() and append()

Using append() on a nested list will add a new list object as an element of the outer list. On the other hand, using extend() on a nested list will add each individual element of the new list as separate elements of the outer list.


new_list = [5, 6]

# Using the append will append it is nested list
my_list.append(new_list)
print(my_list)  

# Output: 
# [1,2,3,4, [5, 6]]

# Using the extend() will append the elements of the list
my_list.extend(new_list)
print(my_list)  

# Output: 
# [1,2,3,4, 5, 6]

5. Number of elements added

When append() is used to add a new element, that element becomes a single element of the list, even if it is itself a list. In contrast, when extend() is used to add multiple elements to a list, each element is added as a separate element in the list.


new_element = ['C++', 'Ruby']

my_list.append(new_element)
print(my_list) 
 
# Output: 
# ['Python', 'Java', ['C++', 'Ruby']


my_list.extend(new_element)
print(my_list)

# Output:
# ['Python', 'Java', 'C++', 'Ruby']

6. Performance of append() vs extend()

append() is faster if you want a few items otherwise, you have to use the extend().


import timeit

def test_append():
    my_list.append('C++')
    my_list.append('Ruby')

def test_extend():
    my_list.extend(['C++', 'Ruby'])

# time the append function
t_append = timeit.Timer('test_append()', 'from __main__ import test_append, my_list')
print(f"append: {t_append.timeit()} seconds")

# time the extend function
t_extend = timeit.Timer('test_extend()', 'from __main__ import test_extend, my_list')
print(f"extend: {t_extend.timeit()} seconds")

7. Summary and Conclusion

We have explained the differences between append() and extend() with lists in Python. You now know, when to use each method, and considering the type of argument, nested lists, number of elements, and performance, you can optimize your code and avoid potential errors. If you have any questions please feel free to leave them below.

Happy coding!