Python Append List to a List Example

How to append a list to another list in Python? We are often required to append a list to another list to create a nested list. Python provides an append() method to append a list as an element to another list. In case you wanted to append elements from one list to another list, you can either use the extend() or insert() with for loop.

1. Quick Examples of Append List to a List

If you are in a hurry, below are some quick examples of appending a list to another list.


# Quick examples of append list to a list

# Example 1: Append list into another list
languages1.append(languages2)

# Example 2: Append multiple lists into another list
languages1.append([languages2,languages3])

# Example 3: Append list elements to list
languages1.extend(languages2)

# Example 4: Append List Elements
# Using insert()
for x in languages2:
    languages1.insert(len(languages1),x)

2. Append List as an Element into Another List

To append the python list as an element into another list, you can use the append() from the list. This takes either string, number, or iterable as an argument and appends it at the end of the list.


# Consider two lists
languages1=['Python','PHP','Java',]
languages2=['C','C++','C#']
print("Language List 1: ",languages1)
print("Language List 2: ",languages2)

# Append list into another list
languages1.append(languages2)
print("Result: ",languages1)

This example yields the below output.

python list insert another list

As you can see, the languages2 list is added as a single element at the end of languages1, creating a nested list. Now, languages1 contains three elements, where the last element is the entire languages2 list.

Similarly, you can also append multiple lists to another list. Using appending a list containing languages2 and languages3 as a single element to languages1. As a result, languages2 and languages3 become nested lists within languages1.


# Consider three lists
languages1=['Python','PHP','Java',]
languages2=['C','C++','C#']
languages3=["Scala","Ruby"]

print("Language List 1: ",languages1)
print("Language List 2: ",languages2)
print("Language List 3: ",languages3)

# Append multiple lists into another list
languages1.append([languages2,languages3])
print("Result: ",languages1)

Yields below output.


# Output:
Language List 1:  ['Python', 'PHP', 'Java']
Language List 2:  ['C', 'C++', 'C#']
Language List 3:  ['Scala', 'Ruby']
Result:  ['Python', 'PHP', 'Java', [['C', 'C++', 'C#'], ['Scala', 'Ruby']]]

3. Append List Elements to Another List

If you wanted to append the individual element from one list to another list you can use the extend(). This method takes the list as an argument and extends the list to separate elements and appends to the list.

In the below example, each element of languages2 is added to the end of languages1, resulting in a flat list without creating a nested structure. The extend() method and += operator both achieve the same result in this context.


# Consider two lists
languages1=['Python','PHP','Java',]
languages2=['C','C++','C#']
print("Language List 1: ",languages1)
print("Language List 2: ",languages2)

# Append List Elements into Another List
languages1.extend(languages2)
print("Result: ",languages1)

Yields below output.


# Output:
Language List 1:  ['Python', 'PHP', 'Java']
Language List 2:  ['C', 'C++', 'C#']
Result:  ['Python', 'PHP', 'Java', 'C', 'C++', 'C#']

Similarly, you can also get this output by using + operator * unpack and many more. Refer to append multiple elements to the list.

4. Using Looping with insert()

Finally, you can also achieve this by using the insert() with for loop. This is used to append a single element at a time at a specific position. Here, we will loop through the languages2 list and each element is appended to the languages1 at the end. len(languages2) returns the count of the list which is used to specify the end position.


# Consider two lists
languages1=['Python','PHP','Java',]
languages2=['C','C++','C#']
print("Language List 1: ",languages1)
print("Language List 2: ",languages2)

# Append list elements using insert()
for x in languages2:
    languages1.insert(len(languages1),x)
print("Result: ",languages1)

This yields the same output as above.

Frequently Asked Questions on Python Append List to a List

How do I append one list to another in Python?

To append one list to another in Python, you can use either the extend() method or the += operator. Both methods modify the original list in place.

Can I append multiple lists as elements to another list?

You can use the append() method to add a list as a single element to another list. If you want to append each element of multiple lists, use a loop with extend() or +=.

How can I append a list as an element to another list using indexing?

To append a list as an element to another list using indexing, you can use the append() method. For example, list2 is appended to the end of list1 as a nested list. The append() method adds the entire list2 as a single element to the end of list1.

Can I append elements from one list to another using a loop?

You can append elements from one list to another using a loop. You can iterate through the elements of one list and append each element to the other list. In the loop, item takes on each value from list2, and list1.append(item) appends each element to the end of list1.

How can I append a list to another list without modifying the original lists?

If you want to concatenate two lists without modifying the original lists, you can create a new list that combines the elements of both lists. You can use the + operator for concatenation.

Conclusion

In this article, you have learned how to append the whole list as an item or element to another list using append() and also learned appending elements from one list to another using extend() and other approaches.

Naveen

Naveen (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn LinkedIn

Leave a Reply