extend() – add all list entries to another list

In this code example, we add all list entries to another list. For this we use the extend() method.

Code Explanation

LineDescription
1Declaration and initialization of the list firstList with the values 1, 2, 3 and 4.
2Declaration and initialization of the list secondList with the values 5, 6, 7 and 8.
4The extend() method extends one list with another. In this case, the second list is appended to the first.
6 + 7With the for loop each list element of firstList is output
firstList = [1, 2, 3, 4]
secondList = [5, 6, 7, 8]

firstList.extend(secondList)

for i in firstList:
    print(i, end=" ")
Output
1 2 3 4 5 6 7 8