Lists vs Tuples for Navigation

I came across something that could likely trip people up if unfamiliar with python.

For those unfamiliar, a list can be changed in size, hold objects, etc and look like this [].
A tuple looks similar but acts quite differently in that it cannot change (size or members) and looks like this ().

What happens is if you call navigate_multiple_endpoints in navigate.py (this function is used by find_path_to_edge in the GameState class) and pass locations as tuples, the function will not return the appropriate path.

For example, with an empty map, testing every (enemy) left start position:

enemy_left_edges = game_state.game_map.get_edge_locations(game_state.game_map.TOP_LEFT)
for location in enemy_left_edges:
    path = game_state.find_path_to_edge(point, self.game_state.game_map.BOTTOM_RIGHT)
    debug_write(path[-1]) #get last point in list

When enemy_left_edges is a tuple, the output is:

SAPlayer 1 my-bot: (14, 0)
SAPlayer 1 my-bot: (14, 0)
SAPlayer 1 my-bot: (14, 0)
SAPlayer 1 my-bot: (14, 0)
SAPlayer 1 my-bot: (14, 0)
SAPlayer 1 my-bot: (14, 0)
SAPlayer 1 my-bot: (14, 0)
SAPlayer 1 my-bot: (14, 0)
SAPlayer 1 my-bot: (14, 0)
SAPlayer 1 my-bot: (14, 0)
SAPlayer 1 my-bot: (14, 0)
SAPlayer 1 my-bot: (14, 0)
SAPlayer 1 my-bot: (14, 0)
SAPlayer 1 my-bot: (14, 0)

When enemy_left_edges is a list, the output is:

SAPlayer 1 my-bot: (26, 12)
SAPlayer 1 my-bot: (14, 0)
SAPlayer 1 my-bot: (20, 6)
SAPlayer 1 my-bot: (25, 11)
SAPlayer 1 my-bot: (15, 1)
SAPlayer 1 my-bot: (16, 2)
SAPlayer 1 my-bot: (22, 8)
SAPlayer 1 my-bot: (18, 4)
SAPlayer 1 my-bot: (27, 13)
SAPlayer 1 my-bot: (21, 7)
SAPlayer 1 my-bot: (19, 5)
SAPlayer 1 my-bot: (17, 3)
SAPlayer 1 my-bot: (23, 9)
SAPlayer 1 my-bot: (24, 10)

You can convert a tuple to a list just by saying list([TUPLE]).
So at the top of def navigate_multiple_endpoints(self, start_point, end_points, game_state) in navigate.py I added the line:

end_points = [[x[0],x[1]] for x in end_points]

I wouldn’t define this as a bug, since everything in the engine has been processed as a list and for most this won’t even come up. I thought I’d post it though to help save some time for those who might happen across this.

I came across it since I have been using tuples so I can use dictionaries to store data with the location as the key, which requires it to be immutable.

4 Likes