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 + 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 works 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 Reply

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