Python List extend method with Example – Python Tutorial

3 weeks ago codedestine 0

In this tutorial, we will learn about python list extend method.
Python List Extend

extend method :

This method is used to add elements of iterable object ( sequence ) like list, string to the end of the list. It iterates through the elements of iterable object passed as an argument and add each element to the end of the list.

Syntax : < List Object >.extend( < iterable Object > ) 

Example:

# Python List extend example # create list object and assign it to variable l l = ['a','b','c','d'] # create list object and assign it to variable ex ex = ['e','f','g'] # call extend() method by passing ex list as an argument l.extend(ex) # print the list, to check its elements print(l) # create one string and assign it to variable s s = 'hijk' # call extend() method by passing s string as an argument l.extend(s) # print the list, to check its elements print(l) # create one tuple and assign it to variable t t = ((1,2),(2,3)) # call extend() method by passing t tuple as an argument l.extend(t) # print the list, to check its elements print(l)

References :-

  1. extend method Docs

That’s all for Python List extend method. If you liked it, please share your thoughts in comments section and share it with others too.

Previous