Python List extend()

Python list‘s extend method adds all the items in passed list to the end of the list.It does not return anything but modifies original list.


Syntax

Here list1 and list2 are objects of the list.


Return

returns nothing, it just make changes to original list.


Example

Let’s see a very simple example:

list1=[1,2,3,4] list2=[5,6,7,8] list1.extend(list2) print("List1:",list1)

Output:

List1: [1, 2, 3, 4, 5, 6, 7, 8]

Let’s add tuple and set to the list using extend.

l=[1,2,3] t=(5,6,7) s={8,10} l.extend(t) l.extend(s) print("List l:",l)

Output:

List l: [1, 2, 3, 5, 6, 7, 8, 10]

That’s all about Python List extend() method.

Leave a Reply

Your email address will not be published. Required fields are marked *