Python List extend()


In this tutorial, we will see about Python List‘s extend method.Python List extend is used to add all items of iterable to end of the list.

Let’s understand about basic of python list first.

Python List is nothing but the collection of elements. You can add any data type to the list.

You can create simple list as below

Python List extend example

You can simply use extend method to add all elements of iterator to the end of the list.Python extend does not return anything but it actually updates the original list.
Let’s understand this with the help of simple example.

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


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

As you can see, elements are actually added to the list rather than whole tuple as opposed to append method.
You can add all items of set,tuple or any other collection to the list.

l=[1,2,3,4] l.extend((5,6)) l.extend({'seven','eight'}) print(l)


[1, 2, 3, 4, 5, 6, ‘seven’, ‘eight’]

As you can see we have added set {‘seven’,’eight’} to the list.All the elements of set and tuples are added to the list using extend method.

That’s all about Python List extend method.


Add Comment