Python Python Collections (2016, retired 2019) Dungeon Game Dungeon Entrance

Flore W
Flore W
4,718 Points

In the Dungeon Game, Kenneth mentions that CELLS could be a tuple instead of a list of tuples. What would it look like?

Does he mean it would be something like (x, y) with x and y being variables?

3 Answers

Stuart McIntosh
Stuart McIntosh
Python Web Development Techdegree Graduate 18,415 Points

Yes, you can have a tuple of tuples as you have highlighted above. I am no expert in Python but I dont this gains you much over a list of tuples.

One thing maybe of benefit is that Tuples are immutable (cant be changed) therefore given that we have fixed set of cells, using a Tuple means that we could not inadevertenly change something., Also tuples are meant to perform better than lists....

Iulia Maria Lungu
Iulia Maria Lungu
16,922 Points

I tried myself to use tuple instead of list. It actually makes sense because that should be a immutable. We don't want to change the CELLS. It's supposed to be constant. I've seen random lib sample works with tuples as well. from the docs: random.sample(population, k) for instance if you feed a dictionary to the random.sample you get an error:

Population must be a sequence or set

So your

CELLS = ((0,0),(0,1),(0,2),
            (1,0),(1,1),(1,2),
            (2,0),(2,1),(2.2))

makes total sense, just that the final cell has a float in it instead of a tuple

Stuart McIntosh
Stuart McIntosh
Python Web Development Techdegree Graduate 18,415 Points

Hi there, yes the cells could look like the following

CELLS = [(0,0),(0,1),(0,2),
            (1,0),(1,1),(1,2),
            (2,0),(2,1),(2.2)]  
Flore W
Flore W
4,718 Points

Hey Stuart, that's a list of tuples, I was more wondering what it would look like as a tuple since Kenneth mentioned it. Could CELLS look like:

CELLS = ((0,0),(0,1),(0,2),
            (1,0),(1,1),(1,2),
            (2,0),(2,1),(2.2)) 

and what would be the difference?