simple question on dictionary usage

Dustan DustanGroups at gmail.com
Sat Oct 27 07:17:18 EDT 2007


On Oct 27, 1:16 am, Frank Millman <fr... at chagford.com> wrote:
> On Oct 27, 8:02 am, Frank Millman <fr... at chagford.com> wrote:
>
> > > This should work -
>
> > > egt = dict([i for i in d.items() if i[0].startswith('E')])
>
> > Of course I meant record.items(), not d.items(). Sorry.
>
> > Frank
>
> On reflection, although my solution is a bit shorter than some others,
> it may not be as efficient, as it retrieves the key and value for
> *every* entry in 'record' before testing whether the key begins with
> 'E'.

That can be fixed by using record.iteritems() instead of
record.items().

> If the dictionary is large, this may be a consideration.
>
> Frank





More information about the Python-list mailing list