extend() in python by R4R Team

-extend() is a python in-build function which is used to add some sequence to the list.
- It extend the list
-It take one argument which is some kind of sequence like list,tuple and set.

Syntax-

list.extend(seq)

Example

list is [1,2,3]
then list.extend([4,5]) will give the [1,2,3,4,5]

list is [1,2,3]
list.extend("Hii") will give [1,2,3,'H','i','i']


program-

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

l.extend((1,2,"Hii"))
print(l)

l.extend(("Hello",2,3))
print(l)

l.extend(["string","string"])
print(l)

l.extend("World")
print(l)


output-

[5, 2, 3, 7, 1, 3, 4, 5]
[5, 2, 3, 7, 1, 3, 4, 5, 1, 2, 'Hii']
[5, 2, 3, 7, 1, 3, 4, 5, 1, 2, 'Hii', 'Hello', 2, 3]
[5, 2, 3, 7, 1, 3, 4, 5, 1, 2, 'Hii', 'Hello', 2, 3, 'string', 'string']
[5, 2, 3, 7, 1, 3, 4, 5, 1, 2, 'Hii', 'Hello', 2, 3, 'string', 'string', 'W', 'o', 'r', 'l', 'd']


-In this program, we take a list which is initially look like [1,2,3] then we extend new items in list and print that list one by one.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!