subscripting Python 3 dicts/getting the only value in a Python 3 dict
Grant Edwards
invalid at invalid.invalid
Wed Jan 13 10:58:23 EST 2016
On 2016-01-13, Steven D'Aprano <steve at 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!! !
More information about the Python-list
mailing list