Skip to content
Related Articles

Related Articles

Improve Article

Python List extend() Method

  • Last Updated : 05 Aug, 2021
Geek Week

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_list
my_list = ['geeks', 'for']
  
# Another list
another_list = [6, 0, 4, 1]
  
# Using extend() method
my_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 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 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 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']

 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




My Personal Notes arrow_drop_up
Recommended Articles
Page :