What is extend() function in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is extend() function in python?

See I know the difference in both append() and extend() function but, I'm confused in extend() function as I'm finding it similar to append...can u tell the example of extend(). Please don't share code just tell me the small example.

31st Jul 2020, 6:54 AM
riya charles
riya charles - avatar
3 Answers
+ 6
a=[1,2,3] a.append([4,5]) print(a) [1,2,3,[4,5]] a.extend([5,6]) print(a) [1,2,3,[4,5],5,6]
31st Jul 2020, 7:43 AM
Abhay
Abhay - avatar
+ 3
Append appends a single item. So if you append a list, it will be in there as a whole. Extend takes an iterable (list, tuple, str whatever) and inserts the elements one by one!
31st Jul 2020, 8:08 AM
HonFu
HonFu - avatar
+ 2
Thanks u all
31st Jul 2020, 8:10 AM
riya charles
riya charles - avatar