Python List extend() method with Examples

What is the Python list extend() method and how to use it? Python List is an excellent and flexible data structure to store and access elements quickly. In some cases, if you want to add multiple elements to the existing list from different iterable like tuples, sets, lists, or a dictionary, you can use the extend() method.

In this article, we will discuss how to add elements from string, tuple, set, list, and dictionary using the extend() method.

1. Quick Examples of List extend()

Following are quick examples of how to use extend() method with the list.


# Quick Examples of extend() method

# Consider the list of strings
fruits=['apple','grapes',]
print("Actual List: ",fruits)

# Extend list with new elements
fruits.extend(['mango','orange'])

# Extend list with tuple
fruits.extend(("pears","custard-apple"))

# Extend list with set
fruits.extend({"guava","cherry","mango"})

# Extend list with dictionary keys
fruits.extend({"fruit1":"pine apple","fruit2":"lemon"})

# Extend list with dictionary values
fruits.extend({"fruit1":"pine apple","fruit2":"lemon"}.values())

# Extend list with string
fruits.extend("papayya")

# Extend list using slicing
fruits[len(fruits):] = ["guava","cherry","mango"]

2. List extend() method

The list.extend() method in Python will add elements to the list. Here each element is added separately to the list one after the other. It will take only one parameter i.e iterable. If we pass string, then it will place each character separately separated by a comma. By using extend() you can add single or multiple elements to the list.

2.1 List extend() Syntax

Let’s see the syntax of the List extend() method.


# Syntax
extend(iterable)

2.2 List extend() Parameters

It can take a string or any iterable like Tuple, List, Set, Dictionary, or List.

2.3 List extend() Return

It will return the List that includes extended elements along with existing elements.

3. Python List extend() – Add Elements

By using the list.extend() method in Python let’s add the elements to the existing list. Note that this modifies the existing list with the new elements and the elements are added at the end of the original list.

Let’s add the elements from list to another list.


# Consider the list of strings
fruits=['apple','mango','guava','lemon']
print("Actual List: ",fruits)

# Extend list
fruits.extend(["orange","papayya","pear"])
print("Final List: ",fruits)

# Output:
# Actual List:  ['apple', 'mango', 'guava', 'lemon']
# Final List:  ['apple', 'mango', 'guava', 'lemon', 'orange', 'papayya', 'pear']

Here, we had four elements in the list and added additional 3 elements from another list. The resultant list contains a total of seven elements.

4. Use extend() with Elements of Tuple

In this example, I will use elements from the tuple to insert into the list. Here, my list had 2 elements to start with, and added a tuple of 3 elements, after adding a tuple, there are 5 elements in the list.


# Consider the list of strings
fruits=["papayya","pear"]
print("Actual List: ",fruits)

# Extend tuple
fruits.extend(('mango','guava','lemon'))
print("Final List: ",fruits)

# Output:
# Actual List:  ['papayya', 'pear']
# Final List:  ['papayya', 'pear', 'mango', 'guava', 'lemon']

5. Use extend() with Set

Let’s have a list of 2 strings and add a Set that to the list using extend() method.

In the below example, there are 2 elements in our list, We specified 2 elements in the set that are duplicates, As the Set doesn’t allow duplicates, it will consider 2 elements as one and add this element to the list. So the total number of elements in the list is 3.


# Consider the list of strings
fruits=['guava','lemon']
print("Actual List: ",fruits)

# Extend set that has duplicates
fruits.extend({"orange","orange"})
print("Final List: ",fruits)

# Output:
# Actual List:  ['guava', 'lemon']
# Final List:  ['guava', 'lemon', 'orange']

6. Add String

Let’s have a list of 3 fruits and add another fruit to this list. As I mentioned in the introduction, adding a string to a list, actually adds each character from the string as an element to the list.


# Consider the list of strings
fruits=['mango','guava','lemon']
print("Actual List: ",fruits)

# Extend string
fruits.extend("papayya")
print("Final List: ",fruits)

# Output:
# Actual List:  ['mango', 'guava', 'lemon']
# Final List:  ['mango', 'guava', 'lemon', 'p', 'a', 'p', 'a', 'y', 'y', 'a']

Previously, there are 3 elements in our list, we added one string – “papayya” to this list using the extend() method. As there are 4 characters in this string, the list will store each character as a single element. Now the final list holds 7 elements.

7. Add Keys/Values of Dictionary

When you use a dictionary as an argument to the list extend() method in Python, it adds keys from the dictionary to the list. If you want to store values in to list, you need to specify values() method along with the dictionary name. It will use only values from the dictionary to the list.

Alternatively, you can also specify keys() method along with the dictionary name to insert keys. It will access only keys from the dictionary and added to the list. By default, it will store only Keys present in a dictionary.

Example 1: Add keys. Here, the keys in the dictionary are – “fruits1” and “fruits2” hence, these are added to the list.


# Consider the list of strings
fruits=["orange","papayya","pear","apple"]
print("Actual List: ",fruits)

# Extend dictionary of keys
fruits.extend({"fruits1":"guava","fruits2":"mango"})
print("Final List: ",fruits)

# Output:
# Actual List:  ['orange', 'papayya', 'pear', 'apple']
# Final List:  ['orange', 'papayya', 'pear', 'apple', 'fruits1', 'fruits2']

Example 2: Add Values. Here, the Values in the dictionary are – “guava” and “mango”. These are added to the list.


# Consider the list of strings
fruits=["orange","papayya","pear","apple"]
print("Actual List: ",fruits)

# Extend dictionary of values
fruits.extend({"fruits1":"guava","fruits2":"mango"}.values())
print("Final List: ",fruits)

# Output:
# Actual List:  ['orange', 'papayya', 'pear', 'apple']
# Final List:  ['orange', 'papayya', 'pear', 'apple', 'guava', 'mango']

8. Slicing to Extend List

Apart from the extend(), we can also add elements to the list through Slicing. Let’s look at the syntax that uses slicing


# Syntax
mylist1[len(mylist1):]=iterable/s

Here, mylist1 is the input list and we can add iterable to this input list.

Let’s create a list of strings and add three strings to the list with the slicing approach.


# Consider the list of strings
fruits=['apple','mango',]
print("Actual List: ",fruits)

# Extend list using Slicing
fruits[len(fruits):] = ["orange","papayya","pear"]
print("Final List: ",fruits)

# Output:
# Actual List:  ['apple', 'mango']
# Final List:  ['apple', 'mango', 'orange', 'papayya', 'pear']

9. Conclusion

In this article, you have learned the syntax and usage of the list extend() method in Python. We have seen how to add elements from different iterable to the list using extend() and Slicing methods. This method inserts elements into the list from another iterable. Here each element is added separately to the list one after the other.

References

Leave a Reply