I've got an issue with lists right now. I've got 2 lists, Checker1 and Checker2, which check for the Ko rule in my game of Go. However, Checker2 should always be checking the board one turn behind Checker1, which checks the board every turn. That way, the Ko rule can be caught and dealt with. To do that, I have a Temp list which gets the contents of Checker1 before each run-through, and set it's contents to Checker2. Then the piece is added to the board. Checker1 gets the contents of the board, and Checker1 and Checker2 are compared to check for the Ko rule.

However, my problem is that for some reason, whenever Checker1 gets the contents of the board, Checker2's contents change to Checker1's contents (I've tested it through printing the 2 lists). Is there any particular reason why? I made copies of all of the lists doing

list1 = list2[:]

But the lists are still having their contents shared. Any idea why this is?

Recommended Answers

All 4 Replies

there must be something else going on. When you make a copy of a list list2 = list1[:] it fills list2 with list1's values, but when you change either list1 or list2, the change isn't mirrored so list1 would stay the same if list2 was changed.

Are you sure your not doing something else incorrect? If you can, post the section of your code were the checker lists are used and changed etc then someone might be able to see if anything else is happening

If you are not changing the contents of one or both of the lists, then use a tuple instead:

tuple1 = tuple(list2)

If your lists have other lists nested in them, then you need to use module copy and make a deepcopy.

Ah, my Python teacher helped me out. Since I was returning the board list through a property in class, and I didn't return it using [:], it kept changing in the middle.

Thanks anyway though!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.