How to add all elements in one list to another list in Python

How to add all elements in one list to another list in Python


In Python you can use extend() The function adds all the elements in one list to another list. extend() The function adds each element in the source list to the target list. Here is an example:

list1 = [1, 2, 3]
list2 = [4, 5, 6]
list2.extend(list1)
print(list2)  # 输出:[4, 5, 6, 1, 2, 3]

In this example, the element list1 is added to list2.

If you want to modify the list in place without creating a new list, you can also use the += operator:

list1 = [1, 2, 3]
list2 = [4, 5, 6]
list2 += list1
print(list2)  # 输出:[4, 5, 6, 1, 2, 3]

Both methods can add all the elements of one list to another list.

General catalog of "AUTOSAR lineage decomposition (ETAS tool chain)"

おすすめ

転載: blog.csdn.net/PlutoZuo/article/details/134697124