top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is difference between append() and extend() method for list in Python?

+1 vote
803 views
What is difference between append() and extend() method for list in Python?
posted Jan 2, 2015 by Amit Kumar Pandey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote
 
Best answer

It is pretty much clear from the name itself. Append means attaching something to the last of a list whereas extend means growing the existing list, Obviously from the last. In both the cases something new is added to the last of the list, but the difference will be more clearer from the example.

first_list = [10,30]
second_list = [100,101]
first_list.append([40,50])
second_list.extend([102,103])
print first_list
[10, 30, [40, 50]]
print second_list
[ 100, 101, 102, 103]
answer Jan 4, 2015 by Prakash
+1 vote

append: Appends object at end
x = [1, 2, 3]
x.append([4, 5])
print (x)
gives you: [1, 2, 3, [4, 5]]

extend: extends list by appending elements from the iterable
x = [1, 2, 3]
x.extend([4, 5])
print (x)
gives you: [1, 2, 3, 4, 5]

answer Dec 27, 2015 by Rajan Paswan
...