Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Extend Function in Python

Last Updated on October 18, 2023 by Ankit Kochar

Python stands out as a widely embraced programming language recognized for its simplicity, readability, and user-friendly nature. Among the factors contributing to Python’s widespread adoption is its extensive collection of built-in functions. Within this array of functions lies the "extend" function, a valuable tool for augmenting lists. This article delves into the intricacies of Python’s extend function, offering an in-depth exploration of its functionality and practical applications.

What is the Extend Function in Python?

Python’s "extend" function is an inherent feature that facilitates the addition of multiple elements to an already existing list. This function accepts a list as its input and appends the elements from the input list to the end of the original list. Furthermore, the extend function in Python is versatile, as it can also be employed to add elements from any iterable object to the original list, not solely limited to lists.

Syntax for Extend Function in Python

listName.extend(iterable)

Here, "listName" is the name of the list to which the elements need to be added, and "iterable" is an iterable object whose elements need to be added to the list.

Parameters of Extend Function in Python

The extend function in python takes only one parameter, an iterable. Iterable can be a list, tuple, string, set, or even a dictionary.

Return Type of Extend Function in Python

The extend Function in Python changes the given list. It does not return anything.

How does the Extend Function Work?

The "extend" function operates by accepting an iterable object and appending its elements to the conclusion of the original list. This iterable object can encompass a variety of data structures, including lists, tuples, strings, sets, or any other object that can be iterated through.

When the "extend" function is called, it first checks if the argument passed to it is an iterable object. If it is not, then the function raises a "TypeError" exception. If the argument is an iterable object, then the elements of the iterable are added to the original list.

Here is an example of how the "extend" function works:

# Creating a list
my_list = [1, 2, 3]


# Using the extend function to add elements to the list
my_list.extend([4, 5, 6])


# Printing the updated list
print(my_list)

Output:

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

Explanation:
In this example, we create a list "my_list" with elements 1, 2, and 3. We then use the "extend" function to add the elements 4, 5, and 6 to the end of the list. Finally, we print the updated list, which will output [1, 2, 3, 4, 5, 6].

Using the Extend Function in Python

The Python "extend" function indeed serves as a valuable asset when handling lists. It empowers you to augment an existing list in two ways: by appending the elements from another list or by incorporating elements from any iterable object. This functionality proves especially beneficial when dealing with extensive datasets or when there’s a need to merge multiple lists into a unified list. Let’s delve into the usage of the "extend()" function with various iterable objects to gain a deeper understanding of its versatility.

  1. Extend Function in Python with a List
    We can add the elements of a list to the end of another list with the help of the extend() Function.

    Code:

    # Creating a list
    my_list = [1, 2, 3]
    list2 = [4, 7, 8]
    
    
    # Using the extend function to add elements to the list
    my_list.extend(list2)
    
    
    # Printing the updated list
    print(my_list)
    

    Output:

    [1, 2, 3, 4, 7, 8]

    Explanation:
    Here we initialized “my_list” with the values 1, 2, and 3 and the list “list2” with 4, 7 and 8 as elements. Then we use the extend function on “my_list” to add elements of “list2”, we get [1, 2, 3, 4, 7, 8] as an output.

  2. Extend Function in Python with Tuple
    We can use the extend Function in Python to add elements from a tuple to the end of a list.

    Code:

    # Creating a list
    my_list = [1, 2, 3]
    my_tuple = (2, 6, 7)
    
    
    # Using the extend function to add elements to the list
    my_list.extend(my_tuple)
    
    
    # Printing the updated list
    print(my_list)
    

    Output:
    [1, 2, 3, 2, 6, 7]

    Explanation:
    In the above code, we have defined a list and a tuple with different elements. Then, we used the extend() function to add the elements of the tuple to the end of the list and thus getting the output as [1, 2, 3, 2, 6, 7].

  3. Extend Function in Python with String
    A List in Python can be extended with the String (all the characters as distinct elements) by using the extend() Function.

    Code:

    # Creating a list
    my_list = [1, 2, 3]
    my_string = "prepbytes"
    
    
    # Using the extend function to add elements to the list
    my_list.extend(my_string)
    
    
    # Printing the updated list
    print(my_list)

    Output:

    [1, 2, 3, 'p', 'r', 'e', 'p', 'b', 'y', 't', 'e', 's']

    Explanation:
    Here, in this example, we have shown how to use the extend() Function with the Strings. When we use the extend() Function, each character of the string gets added to the end of the “my_list” as a different element in list.

  4. Extend Function in Python with Dictionary’s Keys
    When we use the extend() Function with Dictionaries, all the keys in the dictionary get added to the list at the end.

    Code:

    # Creating a list
    my_list = [1, 2, 3]
    my_dict = {'a': 8, 'b': 6}
    
    
    # Using the extend function to add elements to the list
    my_list.extend(my_dict)
    
    
    # Printing the updated list
    print(my_list)

    Output:

    [1, 2, 3, 'a', 'b']

    Explanation:
    In the above code, we have used a Dictionary in the extend() function. As a result of this, all the keys of the dictionary get added in the list as elements.

  5. Extend Function in Python with Dictionary’s Values
    We can also add the values in the Dictionary to the end of a list by using the extend Function in Python.

    Code:

    # Creating a list
    my_list = [1, 2, 3]
    my_dict = {'a': 8, 'b': 6}
    
    
    # Using the extend function to add elements to the list
    my_list.extend(my_dict.values())
    
    
    # Printing the updated list
    print(my_list)

    Output:

    [1, 2, 3, 8, 6]

    Explanation:
    If we want to extend the list with the values in the dictionary instead of the keys, we can do this by using the command “my_list.extend(my_dict.values())”. This statement extends the list with the values present in the dictionary and we get the output as [1, 2, 3, 8, 6].

  6. Extend Function in Python with Set
    Set in Python also behaves the same as a list when we use the extend() function.

    Code:

    # Creating a list
    my_list = [1, 2, 3]
    my_set = {2, 3, 4}
    
    
    # Using the extend function to add elements to the list
    my_list.extend(my_set)
    
    
    # Printing the updated list
    print(my_list)

    Output:

    [1, 2, 3, 2, 3, 4]

    Explanation:
    In the above example, we used a set with the extend() Function. As a result, all the elements of the set get added to the end of the list as different elements and we get the output as [1, 2, 3, 2, 3, 4].

Conclusion
The "extend" function in Python is a versatile and powerful tool for working with lists. It allows you to efficiently add elements to an existing list by appending the elements from another list or incorporating elements from any iterable object. This functionality proves particularly valuable in scenarios involving data manipulation, list merging, and list expansion.

FAQs Related to Extend Function

Below are some of the FAQs related to Extend Function:

1. Can you use the "extend" function to add elements to a list one by one?
No, the "extend" function is designed to add multiple elements at once by appending the elements from another list or iterable object. To add elements individually, you can use the "append" function.

2. What types of iterable objects can be used with the "extend" function?
The "extend" function can be used with a wide range of iterable objects, including lists, tuples, strings, sets, and more.

3. Does the "extend" function modify the original list?
Yes, the "extend" function modifies the original list by adding elements to it. It does not create a new list; instead, it expands the existing one.

4. Is there a difference between the "extend" function and the "+" operator for combining lists?
The "extend" function and the "+" operator can both be used to combine lists. However, the "extend" function modifies the original list, while the "+" operator creates a new list containing the combined elements without modifying the original lists.

5. Are there any performance considerations when using the "extend" function with large lists?
The "extend" function is generally efficient and performs well, even with large lists, as it appends elements in a single operation.

6. Can you use the "extend" function to merge multiple lists into one?
Yes, the "extend" function is commonly used to merge multiple lists into a single list. You can repeatedly apply the "extend" function to add elements from various lists to the same list.

7. Is the "extend" function available in both Python 2.x and Python 3.x?
Yes, the "extend" function is available in both Python 2.x and Python 3.x, making it compatible with both versions of the language.

Leave a Reply

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