Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Jussi Piitulainen Newsgroups: comp.lang.python Subject: Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Date: Tue, 12 Jan 2016 22:18:03 +0200 Organization: A noiseless patient Spider Lines: 24 Message-ID: References: <87egdmd1ou.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="305c68510616a2e7ac08bcd2ff1598bd"; logging-data="31084"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+6ZNOep1x1SHkIYucCvSmj5SmFI28Rn+0=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:xglBBUq0pyAMjkxem32ZuOwTqo4= sha1:Os4FM2WTrHl3pLfDVmbXJkRSrpg= Xref: csiph.com comp.lang.python:101573 Marko Rauhamaa writes: > Jussi Piitulainen: > >> But the most readable thing might be to have a function that extracts >> the sole value by whatever means: >> >> >>> def sole(d): [o] = d.values() ; return o >> ... >> >>> sole(shoe) >> 3.141592653589793 > > In the same vein: > > >>> def sole(d): > ... for o in d.values(): > ... return o > ... > >>> sole(shoe) > 3.141592653589793 Tuple assignment has a useful side effect that all other methods present in this thread lack: it raises an exception if the number of dictionary entries is not exactly one.