Python list of lists

Switch to Dreamhost, and we will move your web site. No kidding.

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,…

A historical timeline of Codango™

Original Source

#codango #developer #development #coder #coding

We're happy to share this resource that we found. The content displayed on this page is property of it's original author and/or their organization.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*