← Back to Chapters

Python Add List Items

? Python Add List Items

⚡ Quick Overview

In Python, lists are dynamic. You can easily add new items at the end, at a specific position, or by merging with other iterables like lists and tuples.

  • append() adds a single item to the end of the list.
  • insert() adds an item at a specific index.
  • extend() adds all items from another iterable (list, tuple, set, etc.).

? Key Concepts

  • Lists are mutable – you can change them after creation.
  • append() and insert() add one item at a time.
  • extend() is used to combine sequences or add multiple items at once.
  • You can extend a list with any iterable, not just other lists.
  • All these methods modify the list in place (they return None).

? Syntax and Behavior

➕ Append Items

Use append(item) to add an item at the end of a list.

? View append() Example
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits)   # ['apple', 'banana', 'cherry']

? Insert at a Specific Position

Use insert(index, item) to add an element at a specific index.

? View insert() Example
fruits = ["apple", "banana", "cherry"]
fruits.insert(1, "orange")
print(fruits)   # ['apple', 'orange', 'banana', 'cherry']

? Extend with Another List

Use extend(iterable) to add all items from another list.

? View extend() with List Example
fruits = ["apple", "banana"]
tropical = ["mango", "pineapple"]
fruits.extend(tropical)
print(fruits)   # ['apple', 'banana', 'mango', 'pineapple']

? Add Any Iterable

extend() also works with tuples, sets, or any other iterable, not just lists.

? View extend() with Tuple Example
fruits = ["apple", "banana"]
fruits.extend(("kiwi", "melon"))
print(fruits)   # ['apple', 'banana', 'kiwi', 'melon']

? Live Output and Explanation

  • After using append("cherry") on ["apple", "banana"], the list becomes ['apple', 'banana', 'cherry'].
  • insert(1, "orange") places "orange" at index 1 and shifts existing items to the right, so you get ['apple', 'orange', 'banana', 'cherry'].
  • extend(tropical) with tropical = ["mango", "pineapple"] adds each item individually, giving ['apple', 'banana', 'mango', 'pineapple'].
  • Extending with a tuple like ("kiwi", "melon") works the same way and produces ['apple', 'banana', 'kiwi', 'melon'].
  • All of these operations modify the original fruits list directly, so no new list is created.

? Tips and Best Practices

  • Use append() for adding single items to the end of a list.
  • Use extend() when combining two sequences or adding multiple items at once.
  • Use insert() when you need to place an item in the middle or at a specific index.
  • Remember that insert() shifts existing elements to the right, which can be slower on large lists.
  • If you write fruits.append(["mango", "pineapple"]), the entire list becomes a single nested item, not merged. Use extend() to avoid nesting.
  • extend() requires an iterable. Using a non-iterable like fruits.extend(5) will raise a TypeError.
  • For performance in tight loops, prefer building a list once with extend() instead of many insert() calls at the front.

? Try It Yourself

  • Create a list of 3 fruits and add a new fruit using append().
  • Insert "grape" at the start of a fruit list using insert().
  • Merge two lists of numbers using extend() and print the result.
  • Extend a list with a tuple of colors, e.g. ("red", "green", "blue").
  • Experiment with append() vs extend() using another list and observe the difference in the final list structure.
  • Write a small program that starts with an empty list and uses all three methods (append, insert, extend) at least once.