Dict lookup shortcut?
Daniel Ellison
daniel at syrinx.net
Tue Oct 12 07:50:44 EDT 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?
>
> Thanks,
>
> M
>
>
I'm not sure what you're trying to do, but you should probably be doing
this instead:
names = {'n1':'Cuthbert', 'n2':'Grub', 'n3':'Dibble'}
for key in names.keys():
print names[key],
No need for the initial list. Alternatively, you could just have all the
names in a list to start with and iterate over that:
names = ['Cuthbert', 'Grub', 'Dibble']
for name in names:
print name,
Dan
NB: all code untested
More information about the Python-list
mailing list