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 February 25, 2023 by Prepbytes

Python is a popular programming language that is known for its simplicity, readability, and ease of use. One of the reasons for Python’s popularity is the vast number of built-in functions that the language provides. One such function is the "extend" function, which is used to add elements to a list. In this article, we will discuss the extend function in python in detail, how it works, and how it can be used.

What is the Extend Function in Python?

The "extend" function is a built-in function in Python that is used to add multiple elements to an existing list. This function takes a list as an argument and appends the elements of the argument list to the end of the original list. The extend Function in Python can also be used to add elements of any iterable object to the original list.

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 works by taking an iterable object and adding its elements to the end of the original list. The iterable object can be a list, tuple, string, set, or any other iterable object.

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 extend function in Python is a useful tool for working with lists. It allows you to add elements to an existing list, either by appending the elements of another list or by adding the elements of any iterable object. This can be particularly helpful when working with large data sets or when you need to combine multiple lists into a single list. Let us understand how to use the extend() Function with various iterable Objects.

  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 useful tool for working with lists. It allows you to add multiple elements to an existing list, either by appending the elements of another list or by adding the elements of any iterable object. This can be particularly helpful when working with large data sets or when you need to combine multiple lists into a single list. The extend function is also more efficient than using the + operator when working with large lists.

Overall, the extend function is a powerful tool that can make your code more efficient and easier to read. By understanding how it works and how to use it effectively, you can improve your Python programming skills and become a more effective developer.

FAQs Related to Extend Function

Ques 1. What is the difference between the "extend" and "append" functions in Python?
Ans. The "extend" function is used to add multiple elements to an existing list, while the "append" function is used to add a single element to the end of a list. When you use the "extend" function, the elements are added individually to the original list, while the "append" function adds the entire object as a single element.

Ques 2. Can I use the "extend" function to add elements to the beginning of a list?
Ans. No, the "extend" function always adds elements to the end of a list. If you want to add elements to the beginning of a list, you can use the "insert" function instead.

Ques 3. What happens if I pass a non-iterable object to the "extend" function?
Ans. If you pass a non-iterable object to the "extend" function, you will get a TypeError. This is because the "extend" function needs an iterable object to be able to iterate over the elements and add them to the original list.

Ques 4. Can I use the "extend" function to add elements to a tuple or a set?
Ans. No, the "extend" function only works with lists. If you want to add elements to a tuple or a set, you can convert them to lists first and then use the "extend" function.

Ques 5. Is the "extend" function faster than using the "+" operator to concatenate lists?
Ans. Yes, the "extend" function is generally faster than using the "+" operator, especially when working with large lists. This is because the "extend" function modifies the original list in place, while the "+" operator creates a new list and copies the elements of both lists into it.

Ques 6. Can I use the "extend" function to add elements to a list in a specific position?
Ans. No, the "extend" function always adds elements to the end of a list. If you want to add elements to a specific position in a list, you can use the "insert" function instead.

Ques 7. Can I use the "extend" function with a string as an argument?
Ans. Yes, you can use the "extend" function to add the characters of a string to a list. The characters will be added as individual elements of the list, not as a single string. If you want to add a string as a single element of the list, you can use the "append" function instead.

Leave a Reply

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