. Advertisement .
..3..
. Advertisement .
..4..
Computers are mostly used to store information and extract information from it. Python is one of the most powerful programming languages. We can use Python to get computers to do most routine processes. This blog is all about how we can append a List to another List in Python.
How Can You Append a List to Another List in Python?
In Python, simply utilize + to append a List to another List. You may quickly expand the list by utilizing the + operator. Now, without wasting time, let’s consider the following instance of this technique:
lst1 = [1,4,6]
lst2 = [2,6,9]
lst3 = [5,8,12]
finallist = lst1 + lst2 + lst3
print(finallist)
Result:
[1, 4, 6, 2, 6, 9, 5, 8, 12]
In Python, simply utilize extend() to append a List to another List. You may quickly expand the list by utilizing extend(). Now, without wasting time, let’s consider the following instance of this technique:
lst1 = [1,4,6]
lst2 = [3,6,10]
lst1.extend(lst2)
print(lst1)
Result:
[1, 4, 6, 3, 6, 10]
1)Utilize extend()
You may quickly expand the list by utilizing extend(). Now, without wasting time, let’s consider the following instance of this technique:
lst1 = [1,4,6]
lst2 = [3,7,9]
lst1.extend(lst2)
print(lst1)
Result:
[1, 4, 6, 3, 7, 9]
2) Utilize +
You may quickly expand the list by utilizing the + operator. Now, without wasting time, let’s consider the following instance of this technique:
lst1 = [1,4,6]
lst2 = [3,5,9]
lst3 = [4,8,10]
finallist = lst1 + lst2 + lst3
print(finallist)
Result:
[1, 4, 6, 3, 5, 9, 4, 8, 10]
3) Utilize for loop
You may also quickly expand the list by utilizing for loop. Now, without wasting time, let’s consider the following instance of this technique:
lst1 = [1,4,6]
lst2 = [5,6,9]
for item in lst2:
lst1.append(item)
print(lst1)
Result:
[1, 4, 6, 5, 6, 9]
4) Utilize insert
You may also quickly expand the list by utilizing insert. Now, without wasting time, let’s consider the following instance of this technique:
mylist = ["pizza","burger","dhosa"]
mylist.insert(2, "momos")
print(mylist)
Result:
['pizza', 'burger','momos', 'dhosa']
5)Utilize intertool.chain()
You may also quickly expand the list by utilizing extend(). Now, without wasting time, let’s consider the following instance of this technique:
import itertools
lst1 = [1,4,6]
lst2 = [3,6,9]
lst3 = [4,8,10]
final_list = list(itertools.chain(lst1, lst2, lst3))
print(final_list)
Result:
[1, 4, 6, 3, 6, 9, 4, 8, 10]
Conclusion
Appending a List to Another List in Python is a confusing problem. We hope this blog has helped clear the air around how to do it. If you have more questions about this topic, please comment below. Thank you for reading; we are always excited when one of our posts can provide useful information on a topic like this!
Leave a comment