Python list extend

Learn about how to use extend() list method in python programming language with suitable examples. This method is capable to adding given item(iterable object) to end of list. Syntax of this method as follows.

list.extend(iterable object)

This method are take one parameter, Such as (string,tuple,list,dictionary etc). And this method are not return any values.

Example of python extend() method

# Extend list example

# Given list x
x = [10,20,30,40,50]

# Given list y
y = ["one","two","three"]

# Given x and y list
print("x :",x) # x : [10, 20, 30, 40, 50]
print("y :",y) # y : ['one', 'two', 'three']

# Adding y list into end of x 
x.extend(y)

# After x and y
print("\nx :",x) # x : [10, 20, 30, 40, 50, 'one', 'two', 'three']
print("y :",y)   # y : ['one', 'two', 'three']

In this example the x.extend(y) statement is adding y list elements to list x. and the resulting list x, y are aggregating the elements from the list. For more clarification you can see below image.

Python list extend method example

Following are the important points which should always be remembered while using extend() method.

  1. This method only adds the iterable object. This method modifies the list to which it is applied. And iterable object (the parameter value of the method) has no effect.
  2. The new size of the modified list depends on the number of elements in the iterable object. Which is a combination of the first and the new elements.

Let's see when adding string by this method how it will working.

# Example B

# Given list x
x = [10,20]

print("x :",x) # x : [10, 20]

# Adding "Like"
x.extend("Like")

# After x 
print("\nx :",x) # x : [10, 20, 'L', 'i', 'k', 'e']

String is an iterable object so it is appending string elements one after the other. Some other examples of understand working concept of this method.

# Example C

# list
x = ["Mango"]

# tuple
y = ("Ice Cream", "Juice")

# dict
z = {"name":"python"}

# Add tuple to list
x.extend(y)

# Add dict to list
x.extend(z)

# Display result
print("x:",x) # x: ['Mango', 'Ice Cream', 'Juice', 'name']
print("y:",y) # y: ('Ice Cream', 'Juice')
print("z:",z) # z: {'name': 'python'}
Python extend method example 2

When parameter value is form of dictionary. So this method it will adding only key. This is a very basic example. When If the iterable object contains another iterable object, this method shares that object. For example.

# Example D

# list x
x = ["Mango"]
# list y
y = [[1,2],[3,5]]

# Add multilist
x.extend(y)

# Display result
print("x:",x) # x: ['Mango', [1, 2], [3, 5]]
print("y:",y) # y: [[1, 2], [3, 5]]
Python extend method example 3

Alternative of python extend method

"+" operator is alternative option to extend method.

# Given list x
x = [10,20,30,40,50]

# Given list y
y = ["one","two","three"]

# Given x and y list
print("x :",x) # x : [10, 20, 30, 40, 50]
print("y :",y) # y : ['one', 'two', 'three']

# Adding y list into end of x 
x += y # x = x + y

# After x and y
print("\nx :",x) # x : [10, 20, 30, 40, 50, 'one', 'two', 'three']
print("y :",y)   # y : ['one', 'two', 'three']



Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2022, kalkicode.com, All rights reserved