What is the difference between Python s list methods append and extend

0 votes
What's the difference between the list methods append() and extend()?
Feb 9, 2022 in Python by Dev
• 6,000 points
674 views

1 answer to this question.

0 votes

Python's list methods append and extend add items to the list but the difference is that
we use append when a single item is to be added in the list
while we use extend when more than one item is to be added.

For Example:

Items = [23,34,56]  #create a list
Items

Output

[23, 34, 56]
Items.append([10,40])  #append one element
Items

Output

[23, 34, 56, [10, 40]]

Python will give an error if we try to append more than one item

Items.append(1,2,3)
Items

Output

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-e5c36eb74f1f> in <module>
----> 1 Items.append(1,2,3)
      2 Items

TypeError: append() takes exactly one argument (3 given)

While in case of Extend more than one item can be added, extend will extend the list without nesting the list, unlike append. 
For Example:

Items = [23,34,56]
Items

Output

[23, 34, 56]
Items.extend([10,20,30])
Items

Output

[23, 34, 56, 10, 20, 30]

Hope this helps!

answered Feb 9, 2022 by Nandini
• 5,480 points

Related Questions In Python

0 votes
1 answer

What is the difference between list and tuple?

Lists are mutable(values can be changed) whereas ...READ MORE

answered May 5, 2018 in Python by aayushi
• 750 points
6,931 views
0 votes
1 answer

What is the difference between python's file I/O system when using 'w' and 'wb'?

Only in Windows, in the latter case, ...READ MORE

answered Sep 11, 2018 in Python by charlie_brown
• 7,720 points
1,465 views
0 votes
5 answers
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,363 views
0 votes
1 answer
0 votes
1 answer

What is the use of "assert" in Python?

The statement assert exists in almost every programming ...READ MORE

answered Feb 7, 2022 in Python by Nandini
• 5,480 points
537 views
0 votes
1 answer

What is the output of the following python code snippet?

for i in range(0, 10, 2):     if ...READ MORE

answered Feb 9, 2022 in Python by Nandini
• 5,480 points
2,455 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP