Dict lookup shortcut?

Mitja nun at example.com
Tue Oct 12 14:22:57 CEST 2004


M. Clift wrote:
> Hi All,
>
> Can someone tell me is there a shorthand version to do
> this?
>
> l1 = ['n1', 'n3', 'n1'...'n23'...etc...]
>
> Names = {'n1':'Cuthbert','n2' :'Grub','n3' :'Dibble'
> etc...}
>
> for name in l1:
>     print Names[name],
>
> Rather than listing all the name+numbers keys in the
> dictionary can these keys be shortened somehow into one
> key and a range?

I'm not sure what you want to do:

l1= [...]
Names = {'n': l1}
print Names[n][12]

Or maybe you meant:

for i in range(1,24):
  Names['n'+`i`]=blah

If that didn't answer your question, please rephrase it. And BTW, variables (like Names) are spelled lowercase (i.e., names) by
convention; it's only class names that usually get capitalized (though it's no fixed rule).





More information about the Python-list mailing list