Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #101627

Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict

From Grant Edwards <invalid@invalid.invalid>
Newsgroups comp.lang.python
Subject Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict
Date 2016-01-13 15:58 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <n75s6v$n1e$1@reader1.panix.com> (permalink)
References <f2711b57-7b11-4fd5-8bab-a3e8581177b5@googlegroups.com> <5695a72a$0$1583$c3e8da3$5496439d@news.astraweb.com>

Show all headers | View raw


On 2016-01-13, Steven D'Aprano <steve@pearwood.info> wrote:
> Probably the best solution, because it will conveniently raise an exception
> if your assumption that the dict has exactly one item is wrong:
>
> item, = d.values()  # Note the comma after "item".

[...]

> but you can unpack a sequence of one item too. If you really want to make it
> obvious that the comma isn't a typo:
>
> (item,) = d.values()

If it were I, I'd definitely do the later.  I used to do it the first
way, but I often times would not notice the comma later when
maintaining the code and end up wasting an embarassing amount of time
when what should have been an easy, trivial change broke.

-- 
Grant Edwards               grant.b.edwards        Yow! It was a JOKE!!
                                  at               Get it??  I was receiving
                              gmail.com            messages from DAVID
                                                   LETTERMAN!!  !

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

subscripting Python 3 dicts/getting the only value in a Python 3 dict Nick Mellor <thebalancepro@gmail.com> - 2016-01-12 08:50 -0800
  RE: subscripting Python 3 dicts/getting the only value in a Python 3 dict Emanuel Barry <vgr255@live.ca> - 2016-01-12 12:00 -0500
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Chris Angelico <rosuav@gmail.com> - 2016-01-13 04:06 +1100
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Terry Reedy <tjreedy@udel.edu> - 2016-01-12 12:12 -0500
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Bernardo Sulzbach <mafagafogigante@gmail.com> - 2016-01-12 15:18 -0200
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Peter Otten <__peter__@web.de> - 2016-01-12 18:32 +0100
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Ian Kelly <ian.g.kelly@gmail.com> - 2016-01-12 10:39 -0700
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-01-12 19:47 +0200
    Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Marko Rauhamaa <marko@pacujo.net> - 2016-01-12 21:42 +0200
      Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-01-12 22:18 +0200
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Peter Otten <__peter__@web.de> - 2016-01-12 19:09 +0100
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Bernardo Sulzbach <mafagafogigante@gmail.com> - 2016-01-12 16:48 -0200
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Peter Otten <__peter__@web.de> - 2016-01-12 19:59 +0100
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Bernardo Sulzbach <mafagafogigante@gmail.com> - 2016-01-12 17:12 -0200
    Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Steven D'Aprano <steve@pearwood.info> - 2016-01-13 12:29 +1100
      Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Bernardo Sulzbach <mafagafogigante@gmail.com> - 2016-01-13 13:53 -0200
  Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Steven D'Aprano <steve@pearwood.info> - 2016-01-13 12:23 +1100
    Re: subscripting Python 3 dicts/getting the only value in a Python 3 dict Grant Edwards <invalid@invalid.invalid> - 2016-01-13 15:58 +0000

csiph-web