reubennott...@gmail.com wrote: > I've been working on a program and have had to halt it due a slight > problem. Here's a basic version of the code: > > a = 'filled' > b = 'filled' > c = 'empty' > d = 'empty' > e = 'filled' > f = 'empty' > g = 'filled' > > testdict = {a : 'apple' , b : 'banana' , c : 'cake' , d : 'damson' , e : > 'eggs' , f : 'fish' , g : 'glue'}
You have duplicate keys here, which becomes obvious when you spell out the values testdict = {"filled": "apple", "filled": "banana", ...} When you do that, the last value ("banana") wins, all others (e. g. "apple") are dropped. > Now what I want to do, is if a variable is filled, print it out. This > however isn't working how I planned. The following doesn't work. > > for fillempt in testdict: > if fillempt == 'filled': > print(testdict[fillempt]) > > All this does though, is print glue, where I'd want it to print: > > apple > banana > eggs > glue > > Perhaps a dictionary isn't the best way to do this.. I wonder what else I > can do... A dictionary is spot-on, but you have to use the unique "apple", "banana",... as keys: >>> status = {"apple": "filled", "banana": "filled", "cake": "empty"} >>> for item in status: ... if status[item] == "filled": ... print(item) ... apple banana Could it be that you just confused dict keys with dict values? -- https://mail.python.org/mailman/listinfo/python-list