How To Append Multiple Values To A List in Python

Do you want to append or add multiple values or items to the list in Python? This article will show you how to append multiple values to a list in Python.

In Python List class has a method named extend that can be used to add multiple values in a list at one time. Suppose you have a list of numbers already and you want to add another list of numbers to the same existing list then you can use extend method.

Also to add a single value to a list in Python there is a method called append. If you want to add only a single value to the list then you should use the append method for a list in Python.

Using Extend Method of List Class

Hence the extend method of list class takes a list of values as an input argument that helps to add those values to the existing list immediately.

Let us see in the below example code the usage of Extend method on a list to add multiple values or items in a list.

#Initializing Small List of Numbers
listNumber = [1,2,3]

#Using Extend Method to Append or Add the to
#The List
listNumber.extend([4,5,6])

#Appending Using Extend and Tuple of Values
listNumber.extend((7,8,9))

#Printing the updated List after Add
print(listNumber)

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

So using the above code I was able to add multiple values to the existing list at once. Also, we saw in the above code that you can either use a list of values or tuple as input to the extend function.

Using Chain Method from Itertools Library of Python

Python has an Itertools library that contains the method called a chain. This method is an iterator that delivers elements from the first iterable until it is exhausted, then advances to the next one, until all iterables are exhausted. for the treatment of successive sequences as one.

Let us see in the below example code the usage of Itertools.chain() method to append multiple items to a list in Python.

#importing the Chain Library
from itertools import chain

#Initializing Small List of Numbers
listNumber = [1,2,3]

#Using chain from itertools library to 
# add multiple Items to The List
appendLists = list(chain(listNumber, [4,5,6]))

#Printing the updated List after Add
print(appendLists)

Output

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

As you can see in the above code, I was successfully able to append two lists using the chain method. This Chain method keeps on append each element from list one and then from list two until all the elements are added to the list.

Using + Operator to Two list in Python

The alternative way can be if you have a list and you want to add multiple values at once then you can simply use the ‘+’ operator to add those values.

Let us see in the below example code the usage of the ‘+’ operator to add multiple items to the list.

#Initializing Small List of Numbers
listNumber = [1,2,3]

#Using + Operator to add multiple Items to
#The List
listNumber = listNumber + [4,5,6]

#Printing the updated List after Add
print(listNumber)

Output:

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

This method is basically concatenating two list items to make it one. Hence you cannot use the ‘+’ operator with tuple if you have a list of numbers.

How To Append Multiple Values To A List in Python

Wrap Up

I hope you were able to get the answer related to how to add multiple values to a list in Python. Since you can either use the extend function or you can use the ‘+’ operator both work in the same ways.

Except when you want to add the values in form of a tuple. In that case, you need to use only the extend function to add to a list.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. How To Reboot or Restart Python Script
  2. Python: To Check If String Contains a Substring
  3. Python: How To Check If a String Contains a List of Substring
  4. How To Return Null in Python Discussed
  5. How to Split a String into a List of Words or Letters in Python

Leave a Comment