In this tutorial, you’ll gain insights into the Python list Extend method, complete with illustrative examples showcasing its application on various sequences.
Important to note: The syntax provided in the following section pertains to Python 3, but it can be adapted to suit any other version of Python you may be using.
To Learn about Lists – Read Python List
Understand the List Extend Method
As we learned from previous tutorials, we can append elements to a list using the append method.
The append works fine when you want to add a single element or a list. But when you wish to append an individual letter from a word or an array of single-digit numbers, then it becomes impossible to achieve. Hence, the extend method comes into the scene to address this limitation.
Extend() Method Syntax
This method updates the list by adding elements to the end. They can be a word or a number, etc. When you call this method, it traverses through the arguments and pushes them into the list one by one to the tail end.
Hence, the number of elements appended is the same as the number of arguments passed. It takes only one parameter and does not have a return value.
Its syntax is as follows:
List_name.extend(element)
After the Python extend method gets called, you will get the updated list object.
Extend Method Time Complexity
It has a time complexity that is proportional to the length of the list that we want to add.
Extend() Function Flowchart
When we pass the element to the extend method as an argument, it gets iterated, and the value from each iteration gets appended to the list.
The flowchart below attempts to explain it in a diagram:
List Extend Method Examples
While you use this method, consider the following points in mind.
a. When you add a “list” or “set” to a list, each element in the list gets iterated and appended to the tail end.
b. When you add a “string” to a list, the letters of the string get iterated and appended to the tail.
List to a List
targetList = ["Python", "CSharp", "Java", "GoLang", "Angular"] listToExtend = ["C", "C++"] targetList.extend(listToExtend) print(targetList) # ['Python', 'CSharp', 'Java', 'GoLang', 'Angular', 'C', 'C++']
Set to a List
targetList = ['Spain', 'France', 'Italy', 'New Zealand'] listToExtend = {'Germany', 'Switzerland'} print(type(listToExtend)) # <class 'set'> targetList.extend(listToExtend) print(targetList) # ['Spain', 'France', 'Italy', 'New Zealand', 'Germany', 'Switzerland']
String to a List
stringsList = ['P', 'Q', 'R'] stringsList.extend('jklmn') print(stringsList) # ['P', 'Q', 'R', 'j', 'k', 'l', 'm', 'n']
Also Read: How to use Python List Insert() Method
Key Facts
Here’s a summary of important facts about the List Extend method in Python presented in a table format for easy understanding:
Fact | Description |
---|---|
Method Name | extend() |
Purpose | To append elements from an iterable (e.g., another list) to an existing list, effectively extending it. |
Input | Accepts a single argument, which is the iterable containing elements to be added to the existing list. |
Modification in Place | Yes, it modifies the original list in place and does not return a new list. |
Return Value | Returns None as it modifies the list directly. |
Example | python myList = [1, 2, 3] myList.extend([4, 5]) # Results in myList = [1, 2, 3, 4, 5] |
Use Cases | Ideal for merging lists or adding elements from an iterable to an existing list without creating a new one. |
Versatility | Works with various iterable types, not just lists (e.g., tuples, strings, other sequences). |
Performance Considerations | More efficient than concatenation (+ ) for large lists due to in-place modification. |
4 Exercises on Python List Extend Method
Here are some unique Python exercises where the list.extend() method can be useful:
Combine Two Lists
Problem#1: Given two lists, list1, and list2, combine them into a single list.
Solution:
list1 = [1, 2, 3] list2 = [4, 5] list1.extend(list2) # Result: list1 = [1, 2, 3, 4, 5]
Add User Inputs to a List
Problem#2: Create an empty list, user_list, and use a loop to add three user inputs (e.g., names) to the list.
Solution:
user_list = [] for _ in range(3): name = input("Enter a name: ") user_list.extend([name]) print(user_list) # Example input: "Alice," "Bob," "Charlie" # Result: user_list = ["Alice", "Bob", "Charlie"]
Merge and Sort Numbers
Problem#3: Create two lists, even_numbers and odd_numbers, containing numbers. Merge them into a single list and then sort the merged list.
Solution:
even_numbers = [2, 4, 6] odd_numbers = [1, 3, 5] merged_numbers = [] merged_numbers.extend(even_numbers) merged_numbers.extend(odd_numbers) merged_numbers.sort() print(merged_numbers) # Result: merged_numbers = [1, 2, 3, 4, 5, 6]
Flatten a List
Problem#4: Given a list of lists, nested_list, use list.extend()
to flatten it into a single list.
Solution:
nested_list = [[1, 2], [3, 4], [5, 6]] flat_list = [] for sublist in nested_list: flat_list.extend(sublist) print(flat_list) # Result: flat_list = [1, 2, 3, 4, 5, 6]
Best,
TechBeamers