What is the Difference Between append and extend in Python?

🆚 Go to Comparative Table 🆚

The main difference between the append() and extend() methods in Python lies in their behavior when adding elements to a list:

  • append():
  • Adds a single element to the end of the list.
  • The element is added as a single item, increasing the list's length by one.
  • Syntax: list.append(element).
  • extend():
  • Adds each element from the iterable (such as a list, tuple, or string) to the list's end individually.
  • The length of the list increases by the number of elements in the iterable.
  • Syntax: list.extend(iterable).

In summary, append() is used to add a single element to the end of a list, while extend() is used to add multiple elements from an iterable to the list's end.

Comparative Table: append vs extend in Python

The main difference between the append() and extend() methods in Python lies in their behavior when adding elements to a list:

  • append(): This method adds a single element to the end of the list. It takes a single element as its argument and returns the updated list. The original list is modified in place.

  • extend(): This method adds multiple elements to the end of the list by concatenating the elements from an iterable (such as a list, tuple, or string) to the existing list. It takes an iterable as its argument and returns the modified list.

Here is a summary of their key differences in a table format:

Feature append() extend()
Effect Adds a single element to the end of the list. Adds multiple individual elements to the end of the list.
Argument Takes a single element as its argument. Takes an iterable (list, tuple, dictionary, set, string) as its argument.

In summary, you should use append() when you want to add a single element to the end of a list, and extend() when you want to add multiple elements to the end of a list by concatenating them from an iterable.