Home >Backend Development >Python Tutorial >How to merge lists in python
How to merge lists in python? Here are three methods:
Open the python language command window, define a list a and use Numerical assignment, as shown in the following figure:
>>> a=[11,22,33,44,55,66,77,88]; >>> a [11, 22, 33, 44, 55, 66, 77, 88]
Define a list b again, and use numerical types to assign values to the list, as shown in the following figure:
>>> b=[12,23,34,45,56]; >>> b [12, 23, 34, 45, 56]
Related recommendations: "Python video tutorial》
The first way, if you want to merge list a and list b, you can directly use a b to generate a new list, as shown in the following figure:
>>> a=[11,22,33,44,55,66,77,88]; >>> a [11, 22, 33, 44, 55, 66, 77, 88] >>> b=[12,23,34,45,56]; >>> b [12, 23, 34, 45, 56] >>> a+b [11, 22, 33, 44, 55, 66, 77, 88, 12, 23, 34, 45, 56]
The second way, then use the append() method to add the b list to the a list, and then view the list a, as shown in the following figure:
>>> a=[11,22,33,44,55,66,77,88]; >>> a [11, 22, 33, 44, 55, 66, 77, 88] >>> b=[12,23,34,45,56]; >>> b [12, 23, 34, 45, 56] >>> a.append(b); >>> a [11, 22, 33, 44, 55, 66, 77, 88, [12, 23, 34, 45, 56]]
The third way, using the same method, you can use the extend method , add a to b, as shown below:
>>> a=[11,22,33,44,55,66,77,88]; >>> a [11, 22, 33, 44, 55, 66, 77, 88] >>> b=[12,23,34,45,56]; >>> b [12, 23, 34, 45, 56] >>> b.extend(a); >>> b [12, 23, 34, 45, 56, 11, 22, 33, 44, 55, 66, 77, 88]
The above is the detailed content of How to merge lists in python. For more information, please follow other related articles on the PHP Chinese website!