Python List extend() Method

The extend() method in Python is a built-in function used to extend a list by adding elements from another list or iterable. The method modifies the list in place, meaning that it adds the elements to the end of the list and does not return a new list. In this article, you will learn how to use the extend() method with examples.

Syntax

list.extend(iterable)

Parameters

  • iterable: The elements to be added to the end of the list. It can be a list, tuple, string, or any other iterable object.

Examples

Example 1: Extend a List with Elements from Another List

Let’s consider two lists and try to extend one list with elements from another list using the extend() method:

>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> a.extend(b)
>>> print(a)
[1, 2, 3, 4, 5, 6]

In this example, we have created two lists a and b. We have used the extend() method to add the elements of the list b to the end of the list a. Finally, we have printed the list a, which now contains six elements including all elements from the list b.

Example 2: Extend a List with Elements from a Tuple

You can also extend a list with elements from a tuple:

>>> a = [1, 2, 3]
>>> b = (4, 5, 6)
>>> a.extend(b)
>>> print(a)
[1, 2, 3, 4, 5, 6]

In this example, we have created a list a and a tuple b. We have used the extend() method to add the elements of the tuple b to the end of the list a. Finally, we have printed the list a, which now contains six elements including all elements from the tuple b.

Example 3: Extend a List with Elements from a String

You can also extend a list with elements from a string:

>>> a = [1, 2, 3]
>>> b = "456"
>>> a.extend(b)
>>> print(a)
[1, 2, 3, '4', '5', '6']

In this example, we have created a list a and a string b. We have used the extend() method to add the elements of the string b to the end of the list a. However, the elements of the string are added as individual characters, not as a whole string. Finally, we have printed the list a, which now contains six elements including all characters from the string b.

It is important to note that the extend() method only adds the elements of the iterable to the end of the list, it does not modify the elements in any way. If you need to modify the elements before adding them to the list, you can use a loop or list comprehension to do so.

Difference between extend() and append()

The extend() and append() methods are both used to add elements to a list in Python, but there are some key differences between them.

  • The extend() method takes an iterable object, such as a list or tuple, and adds its elements to the end of the list. On the other hand, the append() method takes a single object and adds it to the end of the list as a single element.
  • For example, consider the following code:
    >>> a = ["apple", "banana", "cherry"]
    >>> b = ["orange", "peach"]
    >>> a.extend(b)
    >>> print(a)
    ["apple", "banana", "cherry", "orange", "peach"]
    >>> a = ["apple", "banana", "cherry"]
    >>> b = "peach"
    >>> a.append(b)
    >>> print(a)
    ["apple", "banana", "cherry", "peach"]

    Here, the extend() method has added the elements of list b to the end of list a, whereas the append() method has added the single string b as a single element to the end of list a.

  • In summary, use the extend() method to add the elements of an iterable object to a list, and use the append() method to add a single element to the end of a list.

Conclusion

In conclusion, the extend() method is a useful function for adding elements from another iterable to the end of a list in Python. It is a simple and straightforward method that can be used with different types of iterables such as lists, tuples, strings, and more. Understanding the syntax and usage of this method is crucial for effectively manipulating lists in Python.

Leave a Reply

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