Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between list1 = [] list2 = [] and list1 = list2 = [] in python?

I just started using python and I am trying to initialize two lists using list comprehensions. Like this

list1 = list2 = [0.0] * 57

When i do this and insert these lists with values i am getting a different set of values (incorrect values) when compared to the values i get when i initialize these lists seperately. Like

list1 = [0.0] * 57
list2 = [0.0] * 57

What is happening in the first case ? Why am I getting different answers for these 2 cases ?

like image 214
Pradep Avatar asked Dec 06 '25 20:12

Pradep


1 Answers

The first one sets list1 and list2 to both refer to the same list. The second one defines a new list for each name.

like image 155
Daniel Roseman Avatar answered Dec 08 '25 09:12

Daniel Roseman