Home  >  Article  >  Backend Development  >  How to merge lists in python

How to merge lists in python

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-06-25 13:02:534112browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn