[Fixed] python – What is the difference between Python’s list methods append and extend?

Question (Issue)

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

Now we will see solution for issue: What is the difference between Python’s list methods append and extend?


Answer (Solution)

append appends a specified object at the end of the list:

>>> x = [1, 2, 3]
>>> x.append([4, 5])
>>> print(x)
[1, 2, 3, [4, 5]]

extend extends the list by appending elements from the specified iterable:

>>> x = [1, 2, 3]
>>> x.extend([4, 5])
>>> print(x)
[1, 2, 3, 4, 5]

This question is answered By – kender

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0