Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'duplicate': 0.07; 'subject:code': 0.07; 'planned.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:Why': 0.09; 'dict': 0.16; 'keys:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'slight': 0.16; 'values?': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'variable': 0.18; '>>>': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; "i've": 0.25; 'code:': 0.26; 'values': 0.27; 'header:X-Complaints-To:1': 0.27; 'wonder': 0.29; "doesn't": 0.30; 'work.': 0.31; 'keys': 0.31; 'becomes': 0.33; "i'd": 0.34; 'could': 0.34; 'basic': 0.35; 'problem.': 0.35; 'but': 0.35; 'version': 0.36; 'subject:?': 0.36; 'apple': 0.38; 'to:addr:python-list': 0.38; 'that,': 0.38; 'does': 0.39; 'though,': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'email addr:gmail.com': 0.63; 'due': 0.66; 'status:': 0.68; 'obvious': 0.74; 'subject:this': 0.83; 'subject:want': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Why isn't this code working how I want it to? Date: Sat, 12 Oct 2013 11:20:24 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50848404.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1381569606 news.xs4all.nl 15892 [2001:888:2000:d::a6]:40394 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:56736 reubennottage@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?