[Tutor] Program gets stuck after a creating a list from dictinary items!

Prasad, Ramit ramit.prasad at jpmorgan.com
Fri Jul 6 19:04:40 CEST 2012


> > I could resolve it by defining a small function:
> >
> > def getValue(mydict,keys):
> >     A=[];
> >     for i in keys:
> >         A=A+[mydict[i]]
> >     return A
> >
> > and then calling it: D=getValue(prog,sCommonFeatures);
> > (instead of D=[prog[key1] for key1 in list(sCommonFeatures)];)
> >
> > but I'm still surprised why the latter one didn't work!
> 
> It would be more efficient to do the following
> 
> def getValue(mydict, keys):
>     A=[]
>     for i in keys:
>         A.append( mydict[i] )
>     return A

Less efficiently, but maybe more useful if you need to check
against sOtherFeatures you can do the following.

D = [value for key, value in prog.items() if key in sOtherFeatures 
          or key in sCommonFeatures] # Python 3.x syntax


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list