What is the difference between Python’s `append()` and `extend()` methods for lists?

1 Answers
Answered by suresh

Python List Methods: append() vs extend() - Interview Question Answer

Python List Methods: append() vs extend()

In Python, the append() method is used to add a single element to the end of a list, while the extend() method is used to add multiple elements to the end of a list.

Key Differences:

  • append(): Appends an element as a single object to the list.
  • extend(): Appends elements from an iterable (such as a list, tuple, or string) to the list.

It is important to note that while append() adds the specified element as a single object, extend() iterates over the elements in the iterable and adds them individually to the list.

Choose extend() when you want to add multiple elements and append() when you want to add a single element to the list.

Answer for Question: What is the difference between Python’s `append()` and `extend()` methods for lists?