How to add multiple elements to a Python list?

In this post, you will learn how can add multiple elements to a Python list at once.

Python List extend() method

You can use the Python List extend() method to add multiple items to a list. This method adds all the elements of an iterable (list, tuple, set, etc.) to the end of the list.

Syntax of the extend() method

This method takes only one parameter which is an iterable such as list, tuple, set, etc. And this method does not return any value, it just updates the original list.

>>> x = [54, 32, 45]
>>> x.extend(['Hello Python', 85, 29])
>>> print(x)
[54, 32, 45, 'Hello Python', 85, 29]

Inserting Tuple and Set elements into a List

# List
x = ['Awesome']

# Tuple
y = ('Java', 'Python', 'C++')

# Set
z = {12, 2, 45, 33, 2, 45, 19}


['Awesome', 'Java', 'Python', 'C++']
['Awesome', 'Java', 'Python', 'C++', 33, 2, 19, 12, 45]

Leave a Reply

Your email address will not be published. Required fields are marked *