What's the difference between the list methods of Python append() vs extend()?

What’s the difference between the list methods of Python append() vs extend()?

Hey Neha,

There isn’t much difference between append() and extend() , but to help you out, I can brief you on the difference below.

The key difference is that append() adds its argument as a single element to the end of the list, while extend() iterates over its argument and adds each element to the end of the list individually. append() modifies the original list by adding a single element, while extend() modifies the original list by adding multiple elements from an iterable.

append() : append() is used to add a single element to the end of the list.

When you use append(), the argument is added as a single element, even if it’s an iterable (like a list).

my_list = [1, 2, 3]
my_list.append([4, 5])
print(my_list)  # Output: [1, 2, 3, [4, 5]]

extend():

extend() is used to add elements from an iterable (like a list) to the end of the list.

When you use extend(), each iterable element is added individually to the list.

my_list = [1, 2, 3]
my_list.extend([4, 5])
print(my_list)  # Output: [1, 2, 3, 4, 5]

I hope I was able to explain to you the difference between them with simple examples.

Hey Raim

That is a very impressive explanation there; well, I would love to also put this difference in a simple and short way.

In Python, append() and extend() are list methods used to add elements to a list, but they work differently.

.append() Functionality: Adds its argument as a single element to the end of the list. Effect on List Length: Increases by one, regardless of the argument’s type or content. Example:

my_list = ['foo', 'bar']
my_list.append('baz')
print(my_list)

Output: [‘foo’, ‘bar’, ‘baz’]

another_list = [1, 2, 3] my_list.append(another_list) print(my_list)

# Output: ['foo', 'bar', 'baz', [1, 2, 3]]

In this example, another_list is appended as a single element at the end of my_list.

.extend() Functionality: Iterates over its argument, adding each element to the end of the list. Effect on List Length: Increases by the number of elements in the iterable argument.

my_list = [‘foo’, ‘bar’] another_list = [1, 2, 3] my_list.extend(another_list) print(my_list)

# Output: ['foo', 'bar', 1, 2, 3]

Here, each element of another_list is added individually to my_list.

Hey Neha,

Yes, the previous explanation is fabulous,

But here is what I would like to simply: the exploration of append() and extent().

append() adds the entire data as a single element at a new index, while extend() adds each element individually, extending the current list. Example Given two lists: list1 = [123, 456, 678] list2 = [111, 222]

Using append():
list1.append(list2)
print(list1)

Output: [123, 456, 678, [111, 222]]

Here, list2 is added as a single element at the end of list1.

Using extend():

list1.extend(list2)
print(list1)  # Output: [123, 456, 678, 111, 222]

In this case, each element of list2 is added individually to list1.

Performance Consideration An interesting point is that extend() is faster than append() when used in loops. If you have a loop with append(), consider replacing it with list.extend(processed_elements).

Appending new elements can cause the list to be reallocated in memory. Doing this multiple times, especially one element at a time, can degrade performance. Thus, using list.extend is more efficient, similar to how “”.join(stringlist) works for strings.

With performance consideration, it makes it easy for testers or developers to choose the appropriate method.