Open In App

How to Append Multiple Items to a List in Python

Last Updated : 26 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Appending multiple items to a list in Python can be achieved using several methods, depending on whether you want to extend the list with individual elements or nested collections. Let’s explore the various approaches.

Using extend method (Adding multiple items from iterable)

The list.extend() method adds multiple elements to the end of a list, expanding it with the contents of an iterable. The += operator functions similarly to extend(), concatenating the original list with the provided iterable.

Python
# Initializing a list
a = [1, 2, 3]

# Appending multiple items
a.extend([4, 5, 6])
print(a)

# Using += Operator 
b = [1, 2, 3]

# Appending multiple items
b += [4, 5, 6]
print(b)

extend() method is ideal for adding elements from another list or iterable without nesting. Assignment operator approach is concise and achieves the same result as extend().

Using append method

append() is the most simple method and can be used to add items one by one within a loop.

Python
# Initializing a list
a = [1, 2, 3]
b = [4, 5, 6]

# Appending multiple items using a loop
for i in b:
    a.append(i)

print(a)

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

This approach gives more control if you need to manipulate items before appending them.

Using List Comprehension (By creating a new list)

List comprehension can be used to create a new list by combining the existing list with additional items.

Python
# Initializing a list
a = [1, 2, 3]

# Appending multiple items using list comprehension
a = [*a, 4, 5, 6]
print(a)

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

This approach is useful when you want to create a new list while keeping the original list intact.


Next Article

Similar Reads