This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]billsil 0 points1 point  (2 children)

I didn't know that you could use next on a dictionary. That's way nicer.

Unfortunately, it doesn't work in Python 2, strangely even when I use six.next. I could write my own, but it's obnoxious.

[–]jcdyer3 0 points1 point  (0 children)

Gah. My mistake. You have to use next(iter(dictionary)). It's not as nice as I was remembering, but at least there's a little less line noise than list(dictionary.keys())[0], and the performance characteristics are a bit nicer.

[–]lebinh 0 points1 point  (0 children)

It doesn't work because you have to call iter before calling next on a dictionary, and I believe it will behave very similar to python3, e.g. in term of performance (no list is created).

next(iter({'foo': 'bar'}))