Home > Backend Development > Python Tutorial > How to merge lists in python

How to merge lists in python

爱喝马黛茶的安东尼
Release: 2019-06-25 13:02:53
Original
4199 people have browsed it

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]
Copy after login

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]
Copy after login

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]
Copy after login

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]]
Copy after login

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]
Copy after login

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!

Related labels:
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template