Sunday, June 2, 2019

Python list of lists

I just stumbled upon a post/problem in stackoverflow. Here I discuss it and the solution.First check the following code segment : >>>visited = [[False]*4]*4>>>visited[0][1] = True>>>print visited>>> [[False, True, False, False], [False, True, False, False], [False, True, False, False], [False, True, False, False]]Do you see the problem, second element of all the list changed to True! So it means [[False]*4]*4 creates a list that contains reference to the same list [False, False, False,... Original Source

https://codango.com/python-list-of-lists/

I am happy to share this resource that we found. The content displayed on this page is property of its original author or their organization.

No comments:

Post a Comment