Do you want to append list to list in Python ? If yes then you have come at the right place. In this post you will know how to append list to list in python using various examples.
What is a List?
It’s obvious that you must know what is a list. But to those who do not know, the list is a data structure that allows you to store multiple values in a single variable. The list is widely used by most of the coders. It allows you to manipulate the variable inside the list.
How to perform Appending List to List in Python
Now let’s know the various examples for appending a list to a list in Python. Just follow it for a deep understanding.
Example 1: Append list to existing list
Suppose you want to append a new list to the existing list. Then you have to use the append() function. Here you will just pass the new list to the existing list as an argument.
Use the below lines of code to achieve it.
list1 = ["A","B","C"] list2= ["D","E","F","G"] list1.append(list2) print(list1)
Example 2: Appending elements to the list
The above example completely appends the list to another list. But if you want to append only elements to the existing list then you will use the extend() function. It takes a list as an argument.
Run the below lines of code to achieve that.
list1 = ["A","B","C"] list2= ["D","E","F","G"] list1.extend(list2) print(list1)
Example 3: Using the + Operator
The third example to append a list to a list is the use of the + operator. The process is the same as an addition but instead of a particular number it will add the elements to the existing list.
Execute the below lines of code and see the output.
list1 = ["A","B","C"] list2= ["D","E","F","G"] final_list = list1+list2 print(final_list)
In this tutorial, you have learned how to append a list to a list in Python. You have also learned how to append all the elements to the existing list using the extend() function, plus operator and other approaches.
I hope you have liked this tutorial. If you have any query then you can contact us for more information.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.