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,720 points
100,069 views

3 answers to this question.

+5 votes

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]

Hope it helps!!

If you need to know more about Python, It's recommended to join Python course today.

Thanks!

answered Jun 4, 2018 by aryya
• 7,460 points
+5 votes