Python : extend() and append() – Purpose and difference – A Comprehensive Guide with example

python @ Freshers.in

When working with lists in Python, two common methods used for adding elements to a list are extend() and append(). While these methods may seem similar, they serve distinct purposes and understanding the differences between them can help optimize code performance. In this article, we’ll explore the differences between extend() and append() in Python, with detailed examples to illustrate their use cases.

Append() method

The append() method in Python is used to add a single element to the end of a list. This method takes an argument, which can be any object, and appends it to the end of the list. Let’s look at an example:

numbers = [1, 2, 3, 4]
numbers.append(5)
print(numbers)

Output

[1, 2, 3, 4, 5]

As you can see, the append() method has added the integer value 5 to the end of the list.

Extend() method

The extend() method, on the other hand, is used to add multiple elements to the end of a list. This method takes an iterable object (e.g. a list, tuple, or string) and appends each element to the end of the original list. Let’s look at an example:

numbers = [1, 2, 3, 4]
new_numbers = [5, 6, 7]
numbers.extend(new_numbers)
print(numbers)

Output

[1, 2, 3, 4, 5, 6, 7]

As you can see, the extend() method has added all the elements of new_numbers to the end of the original numbers list.

Differences between Append() and Extend()

The main difference between append() and extend() is that append() adds a single element to the end of a list, while extend() adds multiple elements to the end of a list. Another important difference is that append() can add any object to a list, whereas extend() can only add iterable objects (e.g. lists, tuples, and strings).

To better understand these differences, let’s look at some examples:

Example 1: Adding a single element to a list using append()

numbers = [1, 2, 3, 4]
numbers.append('five')
print(numbers)

Output

[1, 2, 3, 4, 'five']

As you can see, we’ve added a string value to the end of the list using append().

Example 2: Adding a single element to a list using extend()

numbers = [1, 2, 3, 4]
numbers.extend('five')
print(numbers)

Output

[1, 2, 3, 4, 'f', 'i', 'v', 'e']

In this example, we’ve used extend() to add the characters of the string ‘five’ to the end of the list. Since a string is iterable in Python, each character is treated as a separate element and added to the list individually.

Example 3: Adding multiple elements to a list using append()

numbers = [1, 2, 3, 4]
numbers.append([5, 6, 7])
print(numbers)

Output

[1, 2, 3, 4,[5, 6, 7]]

In this example, we’ve used append() to add a new list [5, 6, 7] as a single element to the end of the original list.

Example 4: Adding multiple elements to a list using extend()

numbers = [1, 2, 3, 4]
numbers.extend([5, 6, 7])
print(numbers)

Output

[1, 2, 3, 4, 5, 6, 7]

When to use Append() and Extend()

In general, you should use append() when you want to add a single element to the end of a list, and extend() when you want to add multiple elements to the end of a list. If you try to add a list using append(), the list will be treated as a single element and added to the end of the original list. If you try to add a single element using extend(), the element will be treated as an iterable and each of its components will be added as separate elements to the end of the original list.

Differences between append() and extend():

append() adds a single element to the end of a list.
extend() adds multiple elements to the end of a list, one at a time.
append() can add any object to a list.
extend() can only add iterable objects to a list.
If you want to add a single element to a list, use append().
If you want to add multiple elements to a list, use extend().
In conclusion, understanding the differences between append() and extend() can help you write more efficient and effective code when working with lists in Python. Knowing which method to use depending on the situation can save you time and prevent errors in your code.

Refer more on python here :

Author: user

Leave a Reply