Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Date: Tue, 12 Jan 2016 10:39:55 -0700 Lines: 19 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 14IO19YJls/rAcNsLZC5ZgwSTwvZ/HnrBXh4YbCCN4Pw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'subject:Python': 0.05; 'subject:getting': 0.07; 'jan': 0.11; 'exception': 0.13; '2016': 0.16; 'called,': 0.16; 'fully,': 0.16; 'generator.': 0.16; 'propagated': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'reedy': 0.16; 'wrote:': 0.16; 'try:': 0.18; 'am,': 0.23; 'header :In-Reply-To:1': 0.24; 'coding': 0.27; 'message- id:@mail.gmail.com': 0.27; 'values': 0.28; 'raise': 0.29; 'subject:/': 0.30; 'point': 0.33; 'extract': 0.33; 'utility': 0.33; 'tue,': 0.34; 'except': 0.34; 'received:google.com': 0.35; 'next': 0.35; 'desirable': 0.35; 'something': 0.35; 'should': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; '12,': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'subject:the': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'skip:n 10': 0.62; 'more': 0.63; 'subject:value': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=QDGpq4ijRHCkFRM13H81Gb6gcbxETZLaFKZr4sNRDHU=; b=Zf6msEK4G2i92s6iOiBHoCP2m+gNn1thiAzO92CH7zAUon1DHf4zzzjqvPuwtR9bvK xvBPVyREE4ohsRD55IyxyjDM+ksSBgaJGWiQoxuY3Mjx1bP88y0s0SztCJXkoI5d7fjZ jPUKlNtDoT5CKDkgj+E4ZHniScbQhm8kT8Fp3GZQVFumsJ4nok1zSvc8h9b54OSyQ5Bx rXnzSh9IxQR4lrkkxVsqZLpJ3/TV45gf6pSv0f1UGRZz6ObdMkX81am1XQZj68U96dF3 /2GBQvuOB1Pt8epnJ+xccoZunFHZ0uv4Yj0eU5io4EvGIUgasC6ia6T8uBafoq8murGT TJHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=QDGpq4ijRHCkFRM13H81Gb6gcbxETZLaFKZr4sNRDHU=; b=S7OP+26EIlQgkBBgl13jf+fY4wHnOU94zbNxN2TTUJ3z0OxYhYrkkTctOCvaf4p2F7 lP3JNCvK1+NB3xALsl+ThHJdTQ9xfxN7pVlEwBgpOlqauFm0wcjTlTS6k7O8J+StlaOC IBrC4XoDJc4zTVMcXLi3/ER5ehFU1v1FAwKG18kREb0UTmrWFrvosDBhnRTsZoo/9Yh/ 8SVkyvuo1c0NQoP17kgSAdvziC5h/FNkFw129ut5TiHbNQYHvL2rlX6lXA02iL3+/SFR nhcG713iQ5hkMoX5C5QC0oagrGxh4uhKqVgLiscRHi2VVyefpPzyFaybqkUu/Fd6MBQW 3tvA== X-Gm-Message-State: ALoCoQnuCA2rg/1bcWwfyJ8a0ky2/+4c4ebh84I+p7RpczL7h+Q2zti7t8JrxCoeYTEIGMX/L4N0y4DdifXCKCM9Mxzv+MIRcw== X-Received: by 10.50.77.81 with SMTP id q17mr14210217igw.93.1452620434560; Tue, 12 Jan 2016 09:40:34 -0800 (PST) In-Reply-To: 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:101561 On Tue, Jan 12, 2016 at 10:12 AM, Terry Reedy wrote: > Using the values views at intended (as an iterable): > >>>> dv = d.values() >>>> next(iter(dv)) > 1 Good coding practice also dictates that whenever next is called, the potential StopIteration exception must be caught unless it is clearly intended to be propagated up to some generator. So more fully, this should be something like: dv = iter(d.values()) try: next(dv) except StopIteration: raise IndexError("d is empty") At which point it may be desirable to extract that into a utility function.