0

I am trying to create The Game of Life using pygame. I have created the grid and I am able to populate the cells then add them to a 2D list as a boolean (True if populated) All is going accordingly but I want to extract the populated cells and put them into a dictionary and use its coordinates as a key and the boolean as the value

populated_dict = {}
populated_dict[[y_index, x_index]] = arrCells[y_index][x_index]

I want to achieve something like this but I get: TypeError: unhashable type: 'list'. Is there any alternative way of doing this?

1 Answers1

1

You can use a tuple instead of a list and will be able to use it as dict key.

In the example above this would become:

populated_dict = {}
populated_dict[(y_index, x_index)] = arrCells[y_index][x_index]
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176