Python - check for multiple items in 2d array
Question: I have looked around but cannot find anybody asking what I'm trying to do:

Let me give you a bit of background:

I am making an game in python where the player moves around the grid searching
for treasure chests, and I am trying to randomly generate 10 chest locations
in a 8x8 grid.

My grid was created by doing the following:

    
    
    grid = []
    

I then fill the grid with 'subgrids'

Because my game is based around a grid design, this allowed me to separate the
individual rows.

    
    
    for i in range(8):
        grid.append([])
    

This makes me 8 empty lists inside the main 'grid' list.

What I am trying to do next is randomly generate the chest locations, and map
them to another list called 'chestLocations', which also uses 10 subgrids (one
for each unique chest location) . This is so I can create Y and X variables,
which are relative to the grid list.

Here is my GenerateChestLocations() function:

    
    
    def GenerateChestLocations():
    global chestY
    global chestX
    counter = 10
    
    chestY = []
    chestX = []
    
    while counter > 0:
    
        posY = random.randint(0,7)
        posX = random.randint(1,8)
    
        value = GetValue(posY,posX)
    
        if value == gridChar:
            pass
        elif value == playerChar:
            continue
    
        chestY.append(posY)
        chestX.append(posX)
        counter -= 1
    
    for a in range(len(chestY)):
        chestLocations[a].append(chestY[a])
        visitedChests[a].append(chestY[a])
    for i in range(len(chestX)):
        chestLocations[i].append(chestX[i])
        visitedChests[i].append(chestX[i])
    
    for subItem in range(len(visitedChests)):
        visitedChests[subItem].append(0)
    
    return
    

(BTW, the variables used in this are defined at the start of my program, and
are as follows:)

The GetValue() function just returns the value of the grid item for those Y
and X coordinates.

visitedChests is another grid, which needs to be an exact duplicate of
chestLocations, but with an extra item in each 'subgrid' to hold the number of
times that the user has landed on the chest.

My problem is I cannot workout how to detect whether the randomly generated
posY and posX integers are already existing in the chestLocations list.

How do I create detection so if it already finds an item with the same
coordinates, it will just 'continue' to run the whole while loop again?

Thanks for reading btw ;)


Answer: Use the stdlib:

    
    
    import random
    from itertools import product
    
    num_bandits = 5
    num_chests = 10
    
    all_locns = list(product(range(0,8), range(1,9)))
    chest_locns = random.sample(all_locns, num_chests)
    
    unused_locns = [loc for loc in all_locns if loc not in chest_locns]
    bandit_locns = random.sample(unused_locns, num_bandits)
    

