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

Steven D'Aprano steve at pearwood.info
Tue Jan 12 20:29:12 EST 2016


On Wed, 13 Jan 2016 06:12 am, Bernardo Sulzbach wrote:

> I saw it in another answer. next(iter(d)) is still the winner.

Except that doesn't return the *value*, it returns the *key*.

py> d = {'key': 'value'}
py> next(iter(d))
'key'


To get the value:

py> next(iter(d.values()))
'value'

> This resembles a list just too much, making the coder's intent harder
> to understand. This is **very** subjective, of course.

I'm sorry, I don't understand what you mean by "resembles a list"? What
does? In what way?


-- 
Steven




More information about the Python-list mailing list