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 List. The length of the list increases by the number of elements present in the iterable.
Syntax: list.extend(iterable)
Parameters:
- iterable: Any iterable (list, set, tuple, etc.)
Returns:
The extend() method modifies the original list. It doesn’t return any value.
Example 1: Using extend() Method
Python
# my_listmy_list = ['geeks', 'for'] # Another listanother_list = [6, 0, 4, 1] # Using extend() methodmy_list.extend(another_list) print my_list |
Output:
['geeks', 'for', 6, 0, 4, 1]
Example 2: Add Elements of Tuple and Set to List
Python
# My Listmy_list = ['geeks', 'for', 'geeks'] # My Tuplemy_tuple = ('DSA', 'Java') # My Setmy_set = {'Flutter', 'Android'} # Append tuple to the listmy_list.extend(my_tuple) print(my_list) # Append set to the listmy_list.extend(my_set) print(my_list) |
Output:
['geeks', 'for', 'geeks', 'DSA', 'Java'] ['geeks', 'for', 'geeks', 'DSA', 'Java', 'Android', 'Flutter']
Example 3: 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.
Python
# 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']
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


