Python List extend() Method


The Python extend() method adds all items from an iterable to the end of an existing list.

Unlike append(), which adds its argument as one item, extend() reads the iterable and adds its elements individually.

Syntax of extend() 🔝

my_list.extend(iterable)

The extend() method accepts one argument. The argument must be an iterable such as a list, tuple, string, set, dictionary, or range object.

Each item from the iterable is added to the end of the existing list.

Extending a List with Another List 🔝

A common use of extend() is to add the elements of one list to another list.

my_list1=['Alex', 'Ronald', 'John']
my_list2=['Ravi', 'Alen']

my_list1.extend(my_list2)

print(my_list1)
Output
['Alex', 'Ronald', 'John', 'Ravi', 'Alen']

The individual items from my_list2 are added separately to my_list1.

Difference Between extend() and append() 🔝

The main difference is how the supplied object is added.

  • append() adds the complete object as one new list item.
  • extend() adds each element of the iterable separately.

Using append()

my_list=['Alex', 'Ronald', 'John']
my_list2=['Ravi', 'Alen']

my_list.append(my_list2)

print(my_list)
Output
['Alex', 'Ronald', 'John', ['Ravi', 'Alen']]

The complete second list becomes one item inside my_list.

Using extend()

my_list=['Alex', 'Ronald', 'John']
my_list2=['Ravi', 'Alen']

my_list.extend(my_list2)

print(my_list)
Output
['Alex', 'Ronald', 'John', 'Ravi', 'Alen']

Here, 'Ravi' and 'Alen' are added as separate list items.

Extending a List with a String 🔝

A string is iterable, so extend() reads the string one character at a time.

my_list=['a', 'b', 'c', 'd']
my_string='xyz'

my_list.extend(my_string)

print(my_list)
Output
['a', 'b', 'c', 'd', 'x', 'y', 'z']

The string 'xyz' is not added as one string. Its three characters are added separately.

If 'xyz' should be added as one item, use append() instead.

Extending with a Tuple 🔝

A tuple is also iterable, so its elements can be added to a list using extend().

my_list=[1, 2, 3]
my_tuple=(4, 5)

my_list.extend(my_tuple)

print(my_list)
Output
[1, 2, 3, 4, 5]

Extending with range() 🔝

The range() function returns an iterable sequence of integers. These values can be added directly to a list using extend().

my_list=[1, 2]

my_list.extend(range(3, 6))

print(my_list)
Output
[1, 2, 3, 4, 5]

The values generated by range(3,6) are 3, 4, and 5.

Extending with a Set 🔝

A set is iterable, so its elements can also be added to a list.

my_list=[1, 2, 3]
my_set={4, 5}

my_list.extend(my_set)

print(my_list)
Sample Output
[1, 2, 3, 4, 5]

A set does not guarantee a fixed element order, so the order in which 4 and 5 are added should not be relied upon.

Extending with a Dictionary 🔝

A dictionary is iterable too. When a dictionary is passed directly to extend(), its keys are added to the list.

my_list=['Alex']

my_dict={
    'name': 'Ravi',
    'city': 'Delhi'
}

my_list.extend(my_dict)

print(my_list)
Output
['Alex', 'name', 'city']

The dictionary values 'Ravi' and 'Delhi' are not added because iterating over a dictionary directly produces its keys.

Adding Dictionary Values

To add the dictionary values instead, pass my_dict.values() to extend().

my_list=['Alex']

my_dict={
    'name': 'Ravi',
    'city': 'Delhi'
}

my_list.extend(my_dict.values())

print(my_list)
Output
['Alex', 'Ravi', 'Delhi']

Extending an Empty List 🔝

An empty list can be filled with the items of another iterable.

my_list=[]

my_list.extend([1, 2, 3])

print(my_list)
Output
[1, 2, 3]

All three elements from the supplied list are added to the empty list.

Return Value of extend() 🔝

The extend() method modifies the existing list in place and returns None.

my_list=[1, 2]

result=my_list.extend([3, 4])

print(result)
print(my_list)
Output
None
[1, 2, 3, 4]

Do Not Assign extend() Back to the List

Since extend() returns None, assigning its result back to the same variable replaces the list variable with None.

my_list=[1, 2]

my_list=my_list.extend([3, 4])

print(my_list)
Output
None

Call extend() directly when you want to add the iterable's elements to the existing list.

Passing a Non-Iterable to extend() 🔝

The argument supplied to extend() must be iterable. A single integer is not iterable.

my_list=[1, 2]

my_list.extend(3)
Sample Error
TypeError: 'int' object is not iterable

To add the integer 3 as one item, use append(3).

my_list=[1, 2]

my_list.append(3)

print(my_list)
Output
[1, 2, 3]

Summary of extend() 🔝

  • extend() adds all items from an iterable to an existing list.
  • Its syntax is list.extend(iterable).
  • It accepts one iterable argument.
  • Lists, tuples, strings, sets, dictionaries, and range objects can be used.
  • A string is added one character at a time.
  • When a dictionary is used directly, its keys are added.
  • Set element order should not be relied upon.
  • The original list is modified in place.
  • The return value of extend() is None.
  • Use append() when the supplied object should be added as one item.
All List Methods append() insert() Questions with Solutions on List




Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer