Python Forum
Need to refer to Value of a key of a dictionary nested within a dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need to refer to Value of a key of a dictionary nested within a dictionary
#1
You can find my code at the link below, and you can ignore any commented sections of code (Accept the one in make_grid() ):

link removed, code added below

from __future__ import print_function
from random import randint
list = {
    11: {'x': 1, 'y': 1, 'onScreen': 'X', 'occupied': False},
    12: {'x': 1, 'y': 2, 'onScreen': 'X', 'occupied': False},
    13: {'x': 1, 'y': 3, 'onScreen': 'O', 'occupied': True},
    14: {'x': 1, 'y': 4, 'onScreen': 'O', 'occupied': True},
    15: {'x': 1, 'y': 5, 'onScreen': 'O', 'occupied': True},
    16: {'x': 1, 'y': 6, 'onScreen': 'X', 'occupied': False},
    17: {'x': 1, 'y': 7, 'onScreen': 'X', 'occupied': False},
    21: {'x': 2, 'y': 1, 'onScreen': 'X', 'occupied': False},
    22: {'x': 2, 'y': 2, 'onScreen': 'X', 'occupied': False},
    23: {'x': 2, 'y': 3, 'onScreen': 'O', 'occupied': True},
    24: {'x': 2, 'y': 4, 'onScreen': 'O', 'occupied': True},
    25: {'x': 2, 'y': 5, 'onScreen': 'O', 'occupied': True},
    26: {'x': 2, 'y': 6, 'onScreen': 'X', 'occupied': False},
    27: {'x': 2, 'y': 7, 'onScreen': 'X', 'occupied': False},
    31: {'x': 3, 'y': 1, 'onScreen': 'X', 'occupied': True},
    32: {'x': 3, 'y': 2, 'onScreen': 'O', 'occupied': True},
    33: {'x': 3, 'y': 3, 'onScreen': 'O', 'occupied': True},
    34: {'x': 3, 'y': 4, 'onScreen': 'O', 'occupied': True},
    35: {'x': 3, 'y': 5, 'onScreen': 'O', 'occupied': True},
    36: {'x': 3, 'y': 6, 'onScreen': 'O', 'occupied': True},
    37: {'x': 3, 'y': 7, 'onScreen': 'O', 'occupied': True},
    41: {'x': 4, 'y': 1, 'onScreen': 'O', 'occupied': True},
    42: {'x': 4, 'y': 2, 'onScreen': 'O', 'occupied': True},
    43: {'x': 4, 'y': 3, 'onScreen': 'O', 'occupied': True},
    44: {'x': 4, 'y': 4, 'onScreen': 'O', 'occupied': True},
    45: {'x': 4, 'y': 5, 'onScreen': 'O', 'occupied': True},
    46: {'x': 4, 'y': 6, 'onScreen': 'O', 'occupied': True},
    47: {'x': 4, 'y': 7, 'onScreen': 'O', 'occupied': True},
    51: {'x': 5, 'y': 1, 'onScreen': 'O', 'occupied': True},
    52: {'x': 5, 'y': 2, 'onScreen': 'O', 'occupied': True},
    53: {'x': 5, 'y': 3, 'onScreen': 'O', 'occupied': True},
    54: {'x': 5, 'y': 4, 'onScreen': 'O', 'occupied': True},
    55: {'x': 5, 'y': 5, 'onScreen': 'O', 'occupied': True},
    56: {'x': 5, 'y': 6, 'onScreen': 'O', 'occupied': True},
    57: {'x': 5, 'y': 7, 'onScreen': 'O', 'occupied': True},
    61: {'x': 6, 'y': 1, 'onScreen': 'O', 'occupied': False},
    62: {'x': 6, 'y': 2, 'onScreen': 'X', 'occupied': False},
    63: {'x': 6, 'y': 3, 'onScreen': 'X', 'occupied': True},
    64: {'x': 6, 'y': 4, 'onScreen': 'O', 'occupied': True},
    65: {'x': 6, 'y': 5, 'onScreen': 'O', 'occupied': True},
    66: {'x': 6, 'y': 6, 'onScreen': 'O', 'occupied': False},
    67: {'x': 6, 'y': 7, 'onScreen': 'X', 'occupied': False},
    71: {'x': 7, 'y': 1, 'onScreen': 'X', 'occupied': False},
    72: {'x': 7, 'y': 2, 'onScreen': 'X', 'occupied': False},
    73: {'x': 7, 'y': 3, 'onScreen': 'X', 'occupied': True},
    74: {'x': 7, 'y': 4, 'onScreen': 'O', 'occupied': True},
    75: {'x': 7, 'y': 5, 'onScreen': 'O', 'occupied': True},
    76: {'x': 7, 'y': 6, 'onScreen': 'O', 'occupied': False},
    77: {'x': 7, 'y': 7, 'onScreen': 'O', 'occupied': False},
}


# Sends data
class readGrid(object):
    def __init__(self, x, y, boolean, occu):
        self.x = x
        self.y = y
        self.boolean = boolean


# produces the Peg Grid, and outputs it
grid_list = []

list
def make_grid():
    for key, item in list[11]:
        print(item)

        #my_var = readGrid(sub_list[0], sub_list[1], sub_list[2], sub_list[3])
        #grid_list.append(my_var.boolean)
    #print(*grid_list, sep=', ')


make_grid()



#for item in list:
#    if item[11]['onScreen']:
#
#        twoOverKey = str(item['x'] + 2) + str(item['y']);
#        twoOver = list[twoOverKey];
#        if twoOver['occupied'] == False:
#            print("h")
            # do something
What I'm trying to do is print out the Character held in 'onScreen' by for-looping through the dictionary in the make_grid() function. Of course this would be easy if it was a list, but... it's not!
Reply


Messages In This Thread
Need to refer to Value of a key of a dictionary nested within a dictionary - by xepicxmonkeyx - Oct-01-2016, 11:34 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Using Lists as Dictionary Values bfallert 8 326 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  Matching Data - Help - Dictionary manuel174102 1 406 Feb-02-2024, 04:47 PM
Last Post: deanhystad
  Dictionary in a list bashage 2 560 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 695 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  need to compare 2 values in a nested dictionary jss 2 868 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Sort a list of dictionaries by the only dictionary key Calab 1 496 Oct-27-2023, 03:03 PM
Last Post: buran
  python dictionary is that a bug ? rmangla 2 595 Sep-27-2023, 05:52 AM
Last Post: DPaul
  python dictionary syntax nafshar 2 880 Apr-24-2023, 07:26 PM
Last Post: snippsat
  Printing specific values out from a dictionary mcoliver88 6 1,415 Apr-12-2023, 08:10 PM
Last Post: deanhystad
  How to add list to dictionary? Kull_Khan 3 1,012 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020