🐶
Python

Python List append() vs extend(): Key Differences Explained

By Filip on 10/05/2024

Learn the key differences between Python's list methods 'append' and 'extend' and how they impact your data manipulation.

Python List append() vs extend(): Key Differences Explained

Table of Contents

Introduction

In Python, you can add elements to the end of a list using the append() and extend() methods. However, they differ in how they add these elements.

Step-by-Step Guide

In Python, both append() and extend() are used to add elements to the end of a list, but they do so in different ways.

append() adds a single element to the end of a list. This element can be a single value, like a number or a string, or it can be a more complex object, like another list. When you use append() with another list, the entire list is added as a single element, creating a nested list.

extend(), on the other hand, adds multiple elements to the end of a list. It takes an iterable object, such as another list, tuple, or string, and adds each element of that iterable to the original list individually.

Here's a simple way to remember the difference:

  • Use append() when you want to add one thing to the end of a list.
  • Use extend() when you want to add multiple things to the end of a list.

Code Example

This code demonstrates the difference between the append() and extend() methods in Python lists. The append() method adds an entire object to the end of a list, while the extend() method iterates over an iterable (like another list or a string) and adds each element to the end of the list individually.

# Using append()
list1 = [1, 2, 3]
list1.append(4) 
print(list1)  # Output: [1, 2, 3, 4]

list2 = [1, 2, 3]
list2.append([4, 5])  
print(list2)  # Output: [1, 2, 3, [4, 5]] 

# Using extend()
list3 = [1, 2, 3]
list3.extend([4, 5])
print(list3)  # Output: [1, 2, 3, 4, 5]

list4 = [1, 2, 3]
list4.extend("abc")  
print(list4)  # Output: [1, 2, 3, 'a', 'b', 'c'] 

Explanation:

  • append() Examples:
    • We add the integer 4 to list1.
    • We append the list [4, 5] to list2. Notice how it's added as a single element, creating a nested list.
  • extend() Examples:
    • We extend list3 with the elements of the list [4, 5], adding each element individually.
    • We extend list4 with the string "abc". Since strings are iterable, each character is added separately.

Additional Notes

  • Performance: extend() is generally faster than using repeated append() calls in a loop when adding multiple elements.
  • Alternatives:
    • You can achieve similar results to extend() using list concatenation (+ operator) or slicing, but extend() is often more readable and efficient.
    • List comprehensions can be a powerful way to create new lists based on existing iterables, sometimes offering a more concise alternative to append() in loops.
  • Use Cases:
    • append(): Useful for building a list element by element, especially when you need to add items conditionally within a loop.
    • extend(): Ideal for combining lists, adding elements from a tuple or string to a list, or quickly populating a list with multiple values from an iterable.
  • Other Languages: The concepts of append() and extend() are found in other programming languages, often with the same or similar method names. Understanding these methods in Python can be helpful when working with lists or arrays in other languages.

Summary

Method Description Input Result
append() Adds a single element to the end of a list. Single element (any type) Adds the element as a single item to the list.
extend() Adds multiple elements to the end of a list. Iterable (list, tuple, string) Adds each element of the iterable individually to the list.

Conclusion

Choosing between append() and extend() depends on your desired outcome: add a single element as-is with append(), or add elements individually from an iterable with extend(). Understanding this distinction helps you manipulate lists effectively in Python.

References

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait