Home  >  Article  >  Backend Development  >  python list copy

python list copy

高洛峰
高洛峰Original
2016-10-20 09:58:181115browse

If we need to copy a list, we can use a special method, which I will tell you today.

I first define a list

a=[1,2,34]

Then I check the address of the object through the built-in method id()

print id(a)

Object address: 11488352

Then I Copy a list of a and copy it to the variable b

b = a[:]

I output the variable b

print b

The result is [1, 2, 34], which is the same as a, and then I Look at the address in List B below.

print id(b)

Object address: 11511448

It can be seen that the addresses of the two objects are different, indicating that a new list object is generated by re-copying, not the assignment of a reference.


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