Python List extend()

The extend() method adds all the items of the specified iterable, such as list, tuple, dictionary, or string , to the end of a list.

Example

numbers1 = [3, 4, 5]

numbers2 = [10, 20]

# add the items of numbers1 to the number2 list
numbers2.extend(numbers1)

print(f"numbers1 = {numbers1}")
print(f"numbers2 = {numbers2}")

Output

numbers1 = [3, 4, 5]
numbers2 = [10, 20, 3, 4, 5]

Syntax of List extend()

list1.extend(iterable)

The extend() method takes a single argument.

  • iterable - such as list, tuple, string, or dictionary

The extend() doesn't return anything; it modifies the original list.


Example 1: Using extend() Method

languages = ['French', 'English']
languages1 = ['Spanish', 'Portuguese']

# append items of language1 to language
languages.extend(languages1)
print('Languages List:', languages)

Output

Languages List: ['French', 'English', 'Spanish', 'Portuguese']

Example 2: Add Items from Other Iterables

languages = ['French']

languages_tuple = ('Spanish', 'Portuguese')

# add items of the tuple to the languages list
languages.extend(languages_tuple)
print( languages) 

languages_set = {'Chinese', 'Japanese'}

# add items of the set to the languages list
languages.extend(languages_set)
print(languages)

Output

 ['French', 'Spanish', 'Portuguese']
['French', 'Spanish', 'Portuguese', 'Japanese', 'Chinese']

Using + to Extend a List

We can also extend a list using the + operator..

a = [1, 2]
b = [3, 4]

a = a + b

print( a) # [1, 2, 3, 4]

Python extend() Vs append()

If you need to add the item itself (rather than its elements), use the append() method.

a1 = [1, 2]
a2 = [1, 2]
b = (3, 4)

# add items of b to the a1 list
a1.extend(b) #  [1, 2, 3, 4]
print(a1)

# add b itself to the a1 list
a2.append(b)
print(a2)

To learn more, visit list append() method.


Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges