In this code example, we add all list entries to another list. For this we use the
firstListand assigns it the values
[1, 2, 3, 4].
secondListand assigns it the values
[5, 6, 7, 8].
extendmethod on the
firstListobject and passes
secondListas an argument. The
extendmethod is used to add the elements of one list to another list. In this case, all elements of
secondListare added to the end of
forloop in the code iterates over the elements of the list
firstList. For each iteration, the current element is stored in the variable
iduring each iteration of the loop. The
endparameter is set to a space character, so that the numbers are printed on the same line with a space between each number.
firstListhave been processed.
So, the code creates two lists, extends one list with the elements of the other list, and then prints all the elements of the extended list, separated by a space.
firstList = [1, 2, 3, 4] secondList = [5, 6, 7, 8] firstList.extend(secondList) for i in firstList: print(i, end=" ")
1 2 3 4 5 6 7 8