Movatterモバイル変換


[0]ホーム

URL:


Open In App

In Python, extend() method is used to add items from one list to the end of another list. This method modifies the original list by appending all items from the given iterable.

Using extend() method is easy and efficient way to merge two lists or add multiple elements at once.

Let’s look at a simple example of the extend() method.

Python
a=[1,2,3]b=[4,5]# Using extend() to add elements of b to aa.extend(b)print(a)

Output
[1, 2, 3, 4, 5]

Explanation:The elements oflist bare added to the end of list a. The original list ais modified and now contains [1, 2, 3, 4, 5].

Syntax of List extend() Method

list_name.extend(iterable)

Parameters:

  • list_name: The list that will be extended
  • iterable: Any iterable (list, set, tuple, etc.)

Returns:

  • Python List extend() returnsnone.

Using extend() with Different Iterables

The extend() method can work with various types of iterables such as:Lists,Tuples,Sets andStrings (each character will be added separately)

Example:

Python
# Using a tuplea=[1,2,3]b=(4,5)a.extend(b)print(a)# Using a seta=[1,2,3]b={4,5}a.extend(b)print(a)# Using a stringa=['a','b']b="cd"a.extend(b)print(a)

Output
[1, 2, 3, 4, 5][1, 2, 3, 4, 5]['a', 'b', 'c', 'd']

Also Read -Python List Methods

Similar Article on List extend:


Improve

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp