Some text some message..
Back 🔍 extend() Method in Python (List) 23 Jun, 2025

The .extend() method is used to add multiple elements to the end of a list. It unpacks the given iterable (like a list, tuple, or set) and appends each element individually.


📌 Syntax:

list1.extend(iterable)
  • list1: The original list.

  • iterable: A sequence (like list, tuple, set, etc.) whose elements will be added.


✅ Example:

myCart = ['python', 'agentic']
myCart.extend(['langgraph', 'vector_db'])

print(myCart)

Output:

['python', 'agentic', 'langgraph', 'vector_db']

Notice: it added each element from the second list individually (not as a sublist).


🔁 Difference from append():

Method Adds what? Example Resulting List
append() Adds the whole item as one element myCart.append(['a', 'b']) ['python', 'agentic', ['a', 'b']]
extend() Adds each element separately myCart.extend(['a', 'b']) ['python', 'agentic', 'a', 'b']

💡 Works with any iterable:

myCart.extend(('js', 'react'))  # tuple
myCart.extend({'ml', 'ai'})     # set (order not guaranteed)

🛑 Caution:

  • .extend() modifies the original list. It does not return a new list.

  • It raises TypeError if a non-iterable is passed (e.g., myCart.extend(123) is invalid).