AppDividend
Latest Code Tutorials

Python List Extend Example | Python extend() Method

2

Python list extend() is an inbuilt function that adds the specified list elements (or any iterable) to the end of the current list. The extend() extends the list by adding all items of the list (passed as an argument) to an end.

Python List Extend

Python list extend() method appends the contents of seq to list. The extend() method extends the list by adding all elements of a list (passed as an argument) to the end. Here, the items of list2 are added to the end of list1.

Syntax

The syntax for the Python list extend() method is the following.

list.extend(iterable)

Parameters

An iterable parameter is required, and it is iterable like set, tuple, list. The list extend() method does not return any value but adds the content to the existing list.

Let us take an example. My current version of Python is 3.

# app.py

GoT = ['Daenerys', 'Jon', 'Tyrion']
Friends = ['Rachel', 'Monica', 'Phoebe']
GoT.extend(Friends)
print(GoT)

Run the file.

python3 app.py

Python List Extend Example | extend() Method Tutorial

So, we have added one list to the other, and the output will be six items of the list. It is an entirely different behavior then python list append() method example.

In that case, we have only four elements, and the 4th one is added as a whole item.

So, if you kind of merge to lists, then Python list extend() method is beneficial.

List extend: How to append elements of Tuple to List

Let us extend a tuple to the list.

# app.py

GoT1_list = ['Daenerys', 'Jon', 'Tyrion']
Friends1_tuple = ('Rachel', 'Monica', 'Phoebe')
GoT1_list.extend(Friends1_tuple)
print(GoT1_list)

In the above example, we have taken one list and one tuple. Now, we try to add the tuple to a list. The output is the following. It will become a single list.

Add Elements of Tuple To List

Python extend(): Append items of Set To List in Python

Now, we take a Set and add to the List.

# app.py

MJSongs_list = ['Dangerous', 'Smooth criminal', 'Bad']
DrakeSongs_set = {'In my feelings', 'Mia'}
MJSongs_list.extend(DrakeSongs_set)
print(MJSongs_list)

Add Elements of Set To List

It becomes one list and includes all the elements in a single list. 

The Python native datatypes like tuple and set passed to extend() method is automatically converted to a list. And, the items of the list are appended to the end.

Finally, Python List Extend Example is over.

See also

Python list append()

Python list contains

Python add to list

Python list to json

Python list to dictionary

2 Comments
  1. sbabu says

    def my_fun(x):
    for k in range (len(x)):
    print(‘k is ‘,k)
    x.extend(x[:k])
    print(‘x is’,x)
    m = [2,4,3]
    my_fun(m)
    print(m)
    ********************************
    out put

    k is 0
    x is [2, 4, 3] Here it did not do any thing
    k is 1
    x is [2, 4, 3, 2]
    k is 2
    x is [2, 4, 3, 2, 2, 4] Why 2 is added 2 times?
    [2, 4, 3, 2, 2, 4]

  2. Amit Parmar says

    thanks for the post

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.