Skip to content
Related Articles

Related Articles

Python List extend() Method

View Discussion
Improve Article
Save Article
  • Last Updated : 03 Aug, 2022

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 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:

Python3




l = [1, 2, 3]
l.extend([4, 5, 6])
print(l)


Output:

[1, 2, 3, 4, 5, 6]

Example 1: Add Elements of Tuple and Set to List

Using list extend() method to add elements from Tuple or Set to List.

Python




# My List
my_list = ['geeks', 'for', 'geeks']
 
# My Tuple
my_tuple = ('DSA', 'Java')
 
# My Set
my_set = {'Flutter', 'Android'}
 
# Append tuple to the list
my_list.extend(my_tuple)
 
print(my_list)
 
# Append set to the list
my_list.extend(my_set)
 
print(my_list)


Output:

['geeks', 'for', 'geeks', 'DSA', 'Java']
['geeks', 'for', 'geeks', 'DSA', 'Java', 'Android', 'Flutter']

Example 2: 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 list
my_list = ['geeks', 'for', 6, 0, 4, 1]
 
# My string
my_list.extend('geeks')
 
print(my_list)


Output:

['geeks', 'for', 6, 0, 4, 1, 'g', 'e', 'e', 'k', 's']

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!