insert a list into another list in python
- Last modified: 16 March 2020
- Category: python tutorial
In this tutorial, we'll learn how to insert a list in another list with different methods.
Method 1: Using extend()
The extend() is a method which adds the specified list elements on the end of the current list
Syntax
list.extend(iterable)
Example:
# list 1
list_1 = [1, 2, 3, 4, 5]
#list 2
list_2 = [6, 7, 8, 9, 10]
#insert list_2 into list_1 by usind extend() method
list_1.extend(list_2)
#print list 1
print(list_1)
output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Method 2: Using List Concatenation
Syntax
list + list
Example
# list 1
list_1 = [1, 2, 3, 4, 5]
#list 2
list_2 = [6, 7, 8, 9, 10]
#insert list_2 into list_1 by usind List Concatenation
list_1 = list_1 + list_2
#print list 1
print(list_1)
output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
English today is not an art to be mastered it's just a tool to use to get a result