简体   繁体   中英

Explicit type casting in python 2.7 list() function

Given first_list = []

What is the difference between

second_list = first_list

and

third_list = list(first_list) ?

I am having an error in a program in which appending the explicit casting (or list function) to the object yields the correct output. I have stepped through the code and check the types and values, they are the same. cmp(second_list, third_list) yields no differences, yet the addition of the list keyword yields the correct output while the lack of yields an incorrect result. What is the difference between the two?

I'm newer to python, coming from java, very confused.

first_list = [1,2,3,4]
second_list = first_list
second_list[0]=9
print(first_list)
print(second_list)
third_list = list(first_list)
third_list[0]=8
print(third_list)

Run Above code you will understand better Modification in 2nd list will modify 1st list also

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM