Difference between append vs. extend list methods in Python

+1 vote

What's the difference between the list methods append() and extend()?

Jun 4, 2018 in Python by charlie_brown
• 7,760 points
1,371 views

2 answers to this question.

+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]

answered Jun 4, 2018 by aryya
• 7,350 points
+2 votes

Python append() method adds an element to a list, and the extend() method concatenates the first list with another list (or another iterable). When append() method adds its argument as a single element to the end of a list, the length of the list itself will increase by one. Whereas extend() method iterates over its argument adding each element to the list, extending the list. The length of the list will increase by however many elements were in the iterable argument.

answered Aug 21, 2019 by germyrinn
• 180 points
Thanks, @germyrlnn! Good explanation. I have upvoted your answer.

Related Questions In Python

0 votes
1 answer

Difference between append vs. extend list methods in Python

append: Appends object at the end. x = ...READ MORE

answered Aug 8, 2018 in Python by bug_seeker
• 15,390 points
283 views
0 votes
2 answers

In Python, what is difference between Array and List?

Python does not have native array data ...READ MORE

answered Mar 19 in Python by chirag
• 180 points
35,653 views
0 votes
2 answers
0 votes
1 answer

Difference between two lists in python

difference = list(set(list1) - set(list2)) READ MORE

answered May 24, 2018 in Python by Nietzsche's daemon
• 4,260 points
1,019 views
+1 vote
1 answer

What is the difference between range and xrange functions in Python 2.X?

xrange only stores the range params and ...READ MORE

answered Aug 22, 2018 in Python by Priyaj
• 57,530 points
556 views
+7 votes
8 answers

Difference for string comparison in Python: 'is' vs. ==

If we use "==" means both variables ...READ MORE

answered Sep 3, 2018 in Python by Parul Raheja
532 views
+1 vote
1 answer

What's the difference between eval, exec, and compile in Python?

exec is not an expression: a statement ...READ MORE

answered Aug 28, 2018 in Python by Priyaj
• 57,530 points
968 views
0 votes
1 answer

Difference between module and Package in Python

A module is basically a single file ...READ MORE

answered Nov 13, 2018 in Python by SDeb
• 13,230 points
306 views
0 votes
1 answer

Difference between a normal def defined function and lambda function in python?

Have a look at this code: # Python ...READ MORE

answered May 20, 2019 in Python by Trisha
283 views
+1 vote
1 answer

What is the difference between print and return in python?

Return statements end the execution of a ...READ MORE

answered Aug 25, 2019 in Python by anonymous
940 views