Re: Python 3 dict question

2011-05-10 Thread Raymond Hettinger
On May 6, 12:40 pm, dmitrey wrote: > hi all, > suppose I have Python dict myDict and I know it's not empty. > I have to get any (key, value) pair from the dict (no matter which > one) and perform some operation. > In Python 2 I used mere > key, val = myDict.items()[0] > but in Python 3 myDict.item

Re: Dictionary Views -- good examples? [was Re: Python 3 dict question]

2011-05-10 Thread Ethan Furman
Ian Kelly wrote: On Fri, May 6, 2011 at 4:49 PM, Ethan Furman wrote: Anybody care to chime in with their usage of this construct? You should start with PEP 3106. The main idea is that dict.keys() and dict.items() can be treated as frozensets, while still being more lightweight than lists. Th

Re: Dictionary Views -- good examples? [was Re: Python 3 dict question]

2011-05-08 Thread Thomas Rachel
Am 07.05.2011 11:09, schrieb Gregory Ewing: Ethan Furman wrote: Ian Kelly wrote: next(iter(myDict.items())) Which is becoming less elegant. If you're doing this sort of thing a lot you can make a little helper function: def first(x): return next(iter(x)) then you get to say first(myDict

Re: Dictionary Views -- good examples? [was Re: Python 3 dict question]

2011-05-07 Thread Gregory Ewing
Ethan Furman wrote: Ian Kelly wrote: next(iter(myDict.items())) Which is becoming less elegant. If you're doing this sort of thing a lot you can make a little helper function: def first(x): return next(iter(x)) then you get to say first(myDict.items()) -- Greg -- http://mail.pyt

Re: Dictionary Views -- good examples? [was Re: Python 3 dict question]

2011-05-06 Thread Ian Kelly
On Fri, May 6, 2011 at 4:49 PM, Ethan Furman wrote: > Ian Kelly wrote: >> >> On Fri, May 6, 2011 at 1:57 PM, dmitrey wrote: >>> >>> Unfortunately, it doesn't work, it turn out to be dict_items: >> >> next({1:2}.items()) >>> >>> Traceback (most recent call last): >>>  File "", line 1, in

Dictionary Views -- good examples? [was Re: Python 3 dict question]

2011-05-06 Thread Ethan Furman
Ian Kelly wrote: On Fri, May 6, 2011 at 1:57 PM, dmitrey wrote: Unfortunately, it doesn't work, it turn out to be dict_items: next({1:2}.items()) Traceback (most recent call last): File "", line 1, in TypeError: dict_items object is not an iterator So call iter() on it first: next(iter(m

Re: Python 3 dict question

2011-05-06 Thread nirinA raseliarison
[dmitrey] > hi all, > suppose I have Python dict myDict and I know it's not empty. > I have to get any (key, value) pair from the dict (no matter which > one) and perform some operation. > In Python 2 I used mere > key, val = myDict.items()[0] > but in Python 3 myDict.items() return iterator. > Of

Re: Python 3 dict question

2011-05-06 Thread Rob Wolfe
dmitrey writes: > hi all, > suppose I have Python dict myDict and I know it's not empty. > I have to get any (key, value) pair from the dict (no matter which > one) and perform some operation. > In Python 2 I used mere > key, val = myDict.items()[0] > but in Python 3 myDict.items() return iterato

Re: Python 3 dict question

2011-05-06 Thread Ian Kelly
On Fri, May 6, 2011 at 1:57 PM, dmitrey wrote: > Unfortunately, it doesn't work, it turn out to be dict_items: next({1:2}.items()) > Traceback (most recent call last): >  File "", line 1, in > TypeError: dict_items object is not an iterator So call iter() on it first: next(iter(myDict.item

Re: Python 3 dict question

2011-05-06 Thread Ian Kelly
On Fri, May 6, 2011 at 1:51 PM, Chris Rebert wrote: > On Fri, May 6, 2011 at 12:40 PM, dmitrey wrote: >> hi all, >> suppose I have Python dict myDict and I know it's not empty. >> I have to get any (key, value) pair from the dict (no matter which >> one) and perform some operation. >> In Python 2

Re: Python 3 dict question

2011-05-06 Thread dmitrey
On May 6, 10:51 pm, Chris Rebert wrote: > On Fri, May 6, 2011 at 12:40 PM, dmitrey wrote: > > hi all, > > suppose I have Python dict myDict and I know it's not empty. > > I have to get any (key, value) pair from the dict (no matter which > > one) and perform some operation. > > In Python 2 I used

Re: Python 3 dict question

2011-05-06 Thread Chris Rebert
On Fri, May 6, 2011 at 12:40 PM, dmitrey wrote: > hi all, > suppose I have Python dict myDict and I know it's not empty. > I have to get any (key, value) pair from the dict (no matter which > one) and perform some operation. > In Python 2 I used mere > key, val = myDict.items()[0] > but in Python

Python 3 dict question

2011-05-06 Thread dmitrey
hi all, suppose I have Python dict myDict and I know it's not empty. I have to get any (key, value) pair from the dict (no matter which one) and perform some operation. In Python 2 I used mere key, val = myDict.items()[0] but in Python 3 myDict.items() return iterator. Of course, I could use for ke