Javatpoint Logo
Javatpoint Logo

Python List extend() Method

Introduction:

In this article, we are discussing the List extend() method in Python. Python extend() method extends the List by appending all the items from the iterable. Iterable can be a List, Tuple, or Set. Examples are described below.

Syntax:

The syntax of the List extend() method in Python is given below -

Parameters:

The parameter of the List extend() method in Python is x; it is an Iterable type parameter like List, Tuple, or Set.

Return value:

It does not return any value rather modifies the list.

Let's see some examples of extend() method to understand it's functionality.

Python List extend() Method Example 1:

Now give a simple example of the List extend() method in Python. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 10, 20, 30]

Python List extend() Method Example 2:

Now give another simple example of the List extend() method in Python. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

1
2
3
After extending:
1
2
3
4

Python List extend() Method Example 3:

We give another simple example of the List extend() method in Python. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

['Rudra', 'Raja', 'Rahul', 1, 3, 5, 7]

Python List extend() Method Example 4:

We can pass list as an element and the list will be extended. See the example below.

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

1
2
3
After extending:
1
2
3
4
5
6

Python List extend() Method Example 5 :

It can be possible that the element is a tuple type and the list extends itself by tuple elements. See the example below.

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

1
2
3
After extending:
1
2
3
4
5
6

Python List extend() Method Example :

It can be possible that the element is a Set type and the list extends itself by Set elements. See the example below.

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

1
2
3
After extending:
1
2
3
6
5
4

Difference between append() and extend():

If we append a one-dimensional List within a list, then that List will be considered a single element. Another side, if we extend a one-dimensional List within a list, then that List will be considered as individual elements.

Program code:

Here we give a simple example using append() and extend() in Python. The code is given below -

Output:

Now we compile the above code in Python, and after successful compilation, we run it. Then the output is given below -

[1, 2]
[3, 4]
[1, 2, [10, 20, 30]]
x[2]: [10, 20, 30]

[3, 4, 10, 20, 30]
y[2]: 10
y[3]: 20
y[4]: 30

Next TopicPython Lists





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA