Dict lookup shortcut?

Michael Foord fuzzyman at gmail.com
Tue Oct 12 18:11:02 EDT 2004


"M. Clift" <noone at here.com> wrote in message news:<ckgfl4$a9$1 at newsg2.svr.pol.co.uk>...
> 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?
> 

You could just use the integers as keys.

*Or* you could do :
alist = Names.items()
alist.sort()
alist[n][1]   is the nth name
(alist[n] = (nth key, nth name) )

HTH

Fuzzy

> Thanks,
> 
> M



More information about the Python-list mailing list