Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ben Finney Newsgroups: comp.lang.python Subject: Re: Idiom for this case? Date: Fri, 25 Dec 2015 09:30:45 +1100 Lines: 40 Message-ID: References: <86270a67-afff-42a4-a940-9f825fb57146@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de 9LisvumbOUnbUnhXjm01Tg67rVt159GRfWnxu5GwuRhw== Cancel-Lock: sha1:mCjPuwKffqSAFMvrggjJzpXMHeY= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.022 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'one?': 0.05; "'0',": 0.09; 'combines': 0.09; 'finite': 0.09; 'implies': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'semantics': 0.09; 'python': 0.10; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subject:case': 0.16; 'config': 0.18; 'skip': 0.18; 'keys': 0.22; 'header:User-Agent:1': 0.26; 'example': 0.26; 'header:X -Complaints-To:1': 0.26; 'data,': 0.27; 'comparison': 0.29; 'print': 0.30; 'url:python': 0.33; 'equal': 0.34; 'set.': 0.35; 'item': 0.35; 'there': 0.36; 'url:org': 0.36; 'lines': 0.36; 'beginning': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'names': 0.38; 'end': 0.39; 'data': 0.39; 'to:addr:python.org': 0.40; 'url:3': 0.60; 'world': 0.64; 'choose': 0.68; 'skip:\xe2 10': 0.70; '8bit%:46': 0.76; "'2',": 0.84; "'3',": 0.84; '_o__)': 0.84; 'experiment': 0.84; 'idiom': 0.84; 'received:125': 0.84; 'subject:this': 0.85; 'url:reference': 0.91; 'url:tutorial': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100839 KP writes: > Given: > > cfg = {'c': ('3840', '1024'), > 'p1': {'gpio': '1', 'id': '4', 'coord': ('0', '0', '1280', '1024')}, > 'p2': {'gpio': '2', 'id': '5', 'coord': ('1280', '0', '2560', '1024')}, > 'p3': {'gpio': '3', 'id': '6', 'coord': ('2560', '0', '3840', '1024')}} > > for config in cfg: > if config != 'canvas': > print config Given the above data, none of the keys equal 'canvas', so the comparison is redundant if that's the data set. The chosen names are also confusing. It implies that each item of ‘cfg’ is a ‘config’. Can you choose names that better communicate the semantics of that data structure? > Is there an idiom that combines the 'for...' & the 'if..' lines into one? This is an opportunity to learn generator expressions:: for item in (x for x in cfg if x != canvas): print item You will learn about these by working through the Python tutorial, from beginning to end . Don't skip anything, and experiment with each example to understand it before continuing. -- \ “Anyone who believes exponential growth can go on forever in a | `\ finite world is either a madman or an economist.” —Kenneth | _o__) Boulding | Ben Finney