Alternatives to indexing with a diction instead of a list

I am making a Monopoly game and am trying to make it easier for myself and less lines to determine where the character with.

I made a list to show the rents and values of each address
Here it is:

BoardDirectory = {
        'GO': {'name': 'GO'},
        'Old Kent Road': {'name': 'Old Kent Road', 'cost': 60, 'PerHotel': 50, 'SiteRent': 2, 'SiteRentWithColour': 4, 
                          'HouseRent': {'OneHouse': 10, 'TwoHouse': 30, 'ThreeHouse': 90, 'FourHouse': 160, 'Hotel': 250},
                          'LocHouses': 0},
        'Community Chest1': {'name': 'Community Chest'},
        'Whitechapel Road': {'name': 'Whitechapel Road', 'cost': 60, 'PerHotel': 50, 'SiteRent': 4, 'SiteRentWithColour': 8, 
                          'HouseRent': {'OneHouse': 20, 'TwoHouse': 60, 'ThreeHouse': 180, 'FourHouse': 320, 'Hotel': 450},
                          'LocHouses': 0},                                                                                         
        'Income Tax': {'name': 'Kings Cross Station'},
        'Kings Cross Station': {'name': 'Kings Cross Station'},
        'The Angel, Islington': {'name': 'The Angel, Islington', 'cost': 100, 'PerHotel': 50, 'SiteRent': 6, 'SiteRentWithColour': 12, 
                          'HouseRent': {'OneHouse': 30, 'TwoHouse': 90, 'ThreeHouse': 270, 'FourHouse': 400, 'Hotel': 550},
                          'LocHouses': 0},                                                                                        
        'Chance1': {'name': 'Chance'},
        'Euston Road': {'name': 'Euston Road', 'cost': 100, 'PerHotel': 50, 'SiteRent': 6, 'SiteRentWithColour': 12, 
                          'HouseRent': {'OneHouse': 30, 'TwoHouse': 90, 'ThreeHouse': 270, 'FourHouse': 400, 'Hotel': 550},
                          'LocHouses': 0},                                                                                         
        'Pentonville Road': {'name': 'Pentonville Road', 'cost': 120, 'PerHotel': 50, 'SiteRent': 8, 'SiteRentWithColour': 16, 
                          'HouseRent': {'OneHouse': 40, 'TwoHouse': 100, 'ThreeHouse': 300, 'FourHouse': 450, 'Hotel': 600},
                          'LocHouses': 0},                                                                                         
        'JAIL': {'name': 'JAIL'},
        'Pall Mall': {'name': 'Pall Mall', 'cost': 140, 'PerHotel': 100, 'SiteRent': 10, 'SiteRentWithColour': 20, 
                          'HouseRent': {'OneHouse': 50, 'TwoHouse': 150, 'ThreeHouse': 450, 'FourHouse': 625, 'Hotel': 750},
                          'LocHouses': 0},                                                                                         
        'Electric Company': {'name': 'Electric Company'},
        'Whitehall': {'name': 'Whitehall', 'cost': 140, 'PerHotel': 100, 'SiteRent': 10, 'SiteRentWithColour': 20, 
                          'HouseRent': {'OneHouse': 50, 'TwoHouse': 150, 'ThreeHouse': 450, 'FourHouse': 625, 'Hotel': 750},
                          'LocHouses': 0},                                                                                         
        'Northumberland Avenue': {'name': 'Northumberland Avenue', 'cost': 160, 'PerHotel': 100, 'SiteRent': 12, 'SiteRentWithColour': 24, 
                          'HouseRent': {'OneHouse': 60, 'TwoHouse': 180, 'ThreeHouse': 500, 'FourHouse': 700, 'Hotel': 900},
                          'LocHouses': 0},                                                                                         
        'Marylebone Station': {'name': 'Marylebone Station'},
        'Bow Street': {'name': 'Bow Street', 'cost': 180, 'PerHotel': 100, 'SiteRent': 14, 'SiteRentWithColour': 28, 
                          'HouseRent': {'OneHouse': 70, 'TwoHouse': 200, 'ThreeHouse': 550, 'FourHouse': 750, 'Hotel': 950},
                          'LocHouses': 0},                                                                                         
        'Community Chest2': {'name': 'Community Chest2'},
        'Marlborough Street': {'name': 'Marlborough Street', 'cost': 180, 'PerHotel': 100, 'SiteRent': 14, 'SiteRentWithColour': 28, 
                          'HouseRent': {'OneHouse': 70, 'TwoHouse': 200, 'ThreeHouse': 550, 'FourHouse': 750, 'Hotel': 950},
                          'LocHouses': 0},                                                                                         
        'Vine Street': {'name': 'Vine Street', 'cost': 200, 'PerHotel': 100, 'SiteRent': 16, 'SiteRentWithColour': 32, 
                          'HouseRent': {'OneHouse': 80, 'TwoHouse': 220, 'ThreeHouse': 600, 'FourHouse': 800, 'Hotel': 1000},
                          'LocHouses': 0},                                                                                         
        'Free Parking': {'name': 'Free Parking'},
        'Strand': {'name': 'Strand', 'cost': 220, 'PerHotel': 150, 'SiteRent': 18, 'SiteRentWithColour': 36, 
                          'HouseRent': {'OneHouse': 90, 'TwoHouse': 220, 'ThreeHouse': 700, 'FourHouse': 875, 'Hotel': 1050},
                          'LocHouses': 0},                                                                                         
        'Chance2': {'name': 'Chance'},
        'Fleet Street': {'name': 'Fleet Street', 'cost': 220, 'PerHotel': 150, 'SiteRent': 18, 'SiteRentWithColour': 36, 
                          'HouseRent': {'OneHouse': 90, 'TwoHouse': 250, 'ThreeHouse': 700, 'FourHouse': 875, 'Hotel': 1100},
                          'LocHouses': 0},                                                                                         
        'Trafalgar Square': {'name': 'Trafalgar Square', 'cost': 240, 'PerHotel': 150, 'SiteRent': 20, 'SiteRentWithColour': 40, 
                          'HouseRent': {'OneHouse': 100, 'TwoHouse': 300, 'ThreeHouse': 750, 'FourHouse': 925, 'Hotel': 1150},
                          'LocHouses': 0},                                                                                         
        'Fenchurch St Station': {'name': 'Fenchurch St Station'},
        'Leicester Square': {'name': 'Leicester Square', 'cost': 260, 'PerHotel': 150, 'SiteRent': 22, 'SiteRentWithColour': 44, 
                          'HouseRent': {'OneHouse': 110, 'TwoHouse': 330, 'ThreeHouse': 800, 'FourHouse': 975, 'Hotel': 1150},
                          'LocHouses': 0},                                                                                         
        'Coventry Street': {'name': 'Coventry Street', 'cost': 260, 'PerHotel': 150, 'SiteRent': 22, 'SiteRentWithColour': 44, 
                          'HouseRent': {'OneHouse': 110, 'TwoHouse': 330, 'ThreeHouse': 800, 'FourHouse': 975, 'Hotel': 1200},
                          'LocHouses': 0},                                                                                         
        'Water Works': {'name': 'Water Works'},
        'Piccadilly': {'name': 'Piccadilly', 'cost': 280, 'PerHotel': 150, 'SiteRent': 24, 'SiteRentWithColour': 48, 
                          'HouseRent': {'OneHouse': 120, 'TwoHouse': 360, 'ThreeHouse': 850, 'FourHouse': 1025, 'Hotel': 1275},
                          'LocHouses': 0},                                                                                         
        'Go To Jail': {'name': 'Go To Jail'},
        'Regent Street': {'name': 'Regent Street', 'cost': 300, 'PerHotel': 200, 'SiteRent': 26, 'SiteRentWithColour': 52, 
                          'HouseRent': {'OneHouse': 130, 'TwoHouse': 390, 'ThreeHouse': 900, 'FourHouse': 1100, 'Hotel': 1275},
                          'LocHouses': 0},                                                                                         
        'Oxford Street': {'name': 'Oxford Street', 'cost': 300, 'PerHotel': 200, 'SiteRent': 26, 'SiteRentWithColour': 52, 
                          'HouseRent': {'OneHouse': 130, 'TwoHouse': 390, 'ThreeHouse': 900, 'FourHouse': 1100, 'Hotel': 1400},
                          'LocHouses': 0},                                                                                         
        'Community Chest': {'name': 'Community Chest'},
        'Bond Street': {'name': 'Bond Street', 'cost': 320, 'PerHotel': 200, 'SiteRent': 28, 'SiteRentWithColour': 56, 
                          'HouseRent': {'OneHouse': 150, 'TwoHouse': 450, 'ThreeHouse': 1000, 'FourHouse': 1200, 'Hotel': 1500},
                          'LocHouses': 0},                                                                                         
        'Liverpool St Station': {'name': 'Liverpool St Station'},
        'Chance3': {'name': 'Chance'},
        'Park Lane': {'name': 'Park Lane', 'cost': 350, 'PerHotel': 200, 'SiteRent': 35, 'SiteRentWithColour': 70, 
                          'HouseRent': {'OneHouse': 175, 'TwoHouse': 500, 'ThreeHouse': 1100, 'FourHouse': 1300, 'Hotel': 2000},
                          'LocHouses': 0,},                                                                                         
        'Mayfair': {'name': 'Mayfair', 'cost': 400, 'PerHotel': 400, 'SiteRent': 50, 'SiteRentWithColour': 100, 
                          'HouseRent': {'OneHouse': 200, 'TwoHouse': 600, 'ThreeHouse': 1400, 'FourHouse': 1700, 'Hotel': 250,},
                          'LocHouses': 0,},                                                                                         
        }

I am trying to index it, so that it is easier to implement dice rolls to put you in a new location. The code I was trying to use is as follows:

1die = random.randrange(1, 7)
2die = random.randrange(1, 7)
DiceAmount = 1die + 2die
place = 0
place += DiceAmount
currentloc = BoardDirectory.index(place)

But because it is a dictionary it cannot be indexed.
Can anyone offer alternatives?

Probably the simplest change is:

locations = list(BoardDirectory.values())
currentloc = locations[place]

An alternative would be to build the locations list first, and then create your BoardDirectory lookup table after:

locations = [
        {'name': 'GO'},
        {'name': 'Old Kent Road', 'cost': 60, 'PerHotel': 50, 'SiteRent': 2, 'SiteRentWithColour': 4, 
                          'HouseRent': {'OneHouse': 10, 'TwoHouse': 30, 'ThreeHouse': 90, 'FourHouse': 160, 'Hotel': 250},
                          'LocHouses': 0},
        ...
]
BoardDirectory = {loc['name']: loc for loc in locations}

These structures are small enough that making copies in order to afford multiple access methods is fine.

fyi …

can’t name variables starting with a number.

Maybe:

one_die = random.randrange(1, 7)
two_die = random.randrange(1, 7)
DiceAmount = one_die + two_die
place = 0
place += DiceAmount
currentloc = BoardDirectory.index(place)

By the way, by adding the die values, the results will not be unique. Maybe what you meant is to have the results as pairs (i.e., (4,5), (5, 4), etc.), which will be unique, if order matters.

DiceAmount = one_die , two_die

I know you can’t have variables starting with a number, it was just an example. Also the results do not need to be unique it just needs to be a number generator 2 - 12, like if it was two real D6 die.

I think you meant: 2-14

But the dictionary has a total of 39 different main keys. How will you get access to all of the unique keys if the sum of the die can only be between 2-14 … a total of 12 distinct values?

Could be I am missing something here. :wink:

You can re-create the board directory such that the keys are ordered pairs, which if order matters, will be unique:

BoardDirectory = {(1, 3): 'one three', (4, 6): 'four six', (5, 1): 'five one', (7, 2): 'seven two'}
print(BoardDirectory[(1, 3)])

Implementing the board directory with these keys, there will be 49 unique combinations.

No, I meant 2 - 12, seeing as using range(1, 7) doesn’t include the last number so it could only go up to 6.
Run this to see:

for i in range(1, 7):
     print(i)

Also what I am planning to do is keep adding to the player’s indexed position so that you can keep moving around the board.

Yes, you are correct regarding the potential range. I don’t know why I was thinking up to seven since dice have values 1-6. My mistake. Have you tried what @effigies recommended?

Did you mean keys()? As in:

locations = list(BoardDirectory.keys())

No, I meant .values(), but you can also use list(BoardDirectory) if you want a list of the keys. Then you need to update to:

locations = list(BoardDirectory)
currentloc = BoardDirectory[locations[place]]