Python Insert List in Another List

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

1. Quick Examples of Add List Into Another List

Following are quick examples of adding a list to another list.


# Insert list into another list
languages1.append(languages2)

# Insert multiple lists into another list
languages1.append([languages2,languages3])

# Insert list elements to list
languages1.extend(languages2)

# Insert List Elements using insert()
for x in languages2:
    languages1.insert(len(languages1),x)

2. Insert List as an Element into Another List

To insert the whole 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 inserts 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)

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

This example yields the below output.

python list insert another list

Similarly, you can also add multiple lists to another list.


# 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)

# Insert 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. Insert List Elements to Another List

If you wanted to insert or add individual elements 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 adds to the list.


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

# Insert 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. insert() is used to insert a single element at a time at a specific position. Here, we will loop through the languages2 list and each element is added 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)

# Insert List Elements using insert()
for x in languages2:
    languages1.insert(len(languages1),x)
print("Result: ",languages1)

This yields the same output as above.

Conclusion

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

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

You are currently viewing Python Insert List in Another List