Python List extend() Method
The Python’s List extend() method iterates over an iterable like string, list, tuple, etc., and adds each element of the iterable to the end of the Python List, modifying the original list.
Python List extend() Syntax
Syntax: list.extend(iterable)
Parameters:
- iterable: Any iterable (list, set, tuple, etc.)
Returns: None
Python List extend() Example
The list.extend() method is equivalent to list[len(list):] = iterable. The list.extend() is Python’s built-in function that It loops through the provided iterable, appending the elements to the end of the current list.
Python3
l = [1, 2, 3]l.extend([4, 5, 6])print(l) |
Output:
[1, 2, 3, 4, 5, 6]
List extend() Method with a Tuple
Use extend() in Python to add elements from Tuple to List.
Python3
# My Listmy_list = ['geeks', 'for', 'geeks']# My Tuplemy_tuple = ('DSA', 'Java')# Append tuple to the listmy_list.extend(my_tuple)print(my_list) |
Output:
['geeks', 'for', 'geeks', 'DSA', 'Java']
List extend() Method with a Set
Use extend() in Python to add elements from Set to List.
Python3
# My Listmy_list = ['geeks', 'for', 'geeks']# My Setmy_set = {'Flutter', 'Android'}# Append set to the listmy_list.extend(my_set)print(my_list) |
Output:
['geeks', 'for', 'geeks', 'Flutter', 'Android']
Extend String to the List
A string is iterable, so if you extend a list with a string, you’ll append each character as you iterate over the string.
Python3
# My listmy_list = ['geeks', 'for', 6, 0, 4, 1]# My stringmy_list.extend('geeks')print(my_list) |
Output:
['geeks', 'for', 6, 0, 4, 1, 'g', 'e', 'e', 'k', 's']
Related Article: Difference between Append, Extend, and Insert in Python



Please Login to comment...