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
#2
Quote: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!
You can loop through a dictionary too

for k,v in list.items():
	print(v['onScreen'])
You also do not need to import future print on 2.7.10

and you should not overwrite "list" built-in

https://repl.it/X3G/4836
Recommended Tutorials:
Reply
#3
(Oct-02-2016, 01:00 AM)metulburr Wrote:
Quote: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!
You can loop through a dictionary too

for k,v in list.items():
	print(v['onScreen'])
You also do not need to import future print on 2.7.10

and you should not overwrite "list" built-in

https://repl.it/X3G/4836

This worked like a charm, had it printing out randomly at the start (don't know why), but got it working in the end!
Reply
#4
(Oct-02-2016, 01:00 AM)metulburr Wrote: You also do not need to import future print on 2.7.10

but if you are giving away (or selling) code for others to just run, it might still be needed for a system that has 2.6 :(
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
(Oct-02-2016, 05:40 AM)Skaperen Wrote:
(Oct-02-2016, 01:00 AM)metulburr Wrote: You also do not need to import future print on 2.7.10

but if you are giving away (or selling) code for others to just run, it might still be needed for a system that has 2.6 :(

Thats why i specifically said 2.7.10. 
2.6 is quite old. 

And if you are giving away or selling code for others to run you should build an executable with it so that they dont even need python. Then you can attach whichever python interpreter your program needs with it.
Recommended Tutorials:
Reply
#6
(Oct-02-2016, 02:26 AM)xepicxmonkeyx Wrote:
(Oct-02-2016, 01:00 AM)metulburr Wrote:
Quote: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!
You can loop through a dictionary too

for k,v in list.items():
	print(v['onScreen'])
You also do not need to import future print on 2.7.10

and you should not overwrite "list" built-in

https://repl.it/X3G/4836

This worked like a charm, had it printing out randomly at the start (don't know why), but got it working in the end!

I mentioned it printed out the dict randomly. For the printing of the grid that wasn't something I needed. But how do I make it do so when I do?
Reply
#7
dict have no order(unless using ordered dict), so will be printed randomly, to print the values in an order the key will have to be used. A list will always print in the same order.
Reply
#8
You can use collections.OrderdDict to maintain the order
https://pymotw.com/2/collections/ordereddict.html
Recommended Tutorials:
Reply
#9
In version 3.6 dicts will be ordered
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
(Oct-02-2016, 03:42 PM)wavic Wrote: In version 3.6 dicts will be ordered

Oh my, that's some news!
Sorry for being ignorant and not up to date with news... do you have a source for this info?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Matching Data - Help - Dictionary manuel174102 1 357 Feb-02-2024, 04:47 PM
Last Post: deanhystad
  Dictionary in a list bashage 2 496 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 598 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  need to compare 2 values in a nested dictionary jss 2 798 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  Sort a list of dictionaries by the only dictionary key Calab 1 452 Oct-27-2023, 03:03 PM
Last Post: buran
  python dictionary is that a bug ? rmangla 2 547 Sep-27-2023, 05:52 AM
Last Post: DPaul
  python dictionary syntax nafshar 2 840 Apr-24-2023, 07:26 PM
Last Post: snippsat
  Printing specific values out from a dictionary mcoliver88 6 1,317 Apr-12-2023, 08:10 PM
Last Post: deanhystad
  How to add list to dictionary? Kull_Khan 3 951 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
  Dictionary freethrownucleus 3 65,401 Mar-22-2023, 08:26 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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