[Tutor] A replacement for a "for" loop

John Fouhy john at fouhy.net
Wed Aug 29 04:12:35 CEST 2007


On 29/08/07, Trey Keown <trey at opmstech.org> wrote:
> attrs={u'title': u'example window title', u'name': u'SELF', u'icon':
> u'e.ico'}
> keys = ['name','title','icon']
> for (tag, val) in attrs.iteritems():
>     for key in keys:
>         print val
>
> the first "for" tag causes the dictionary (attrs) to have its keys called
> "tag" and its value called "val". The second "for" loop causes the
> dictionary keys to be read in a certain order. How could I take away the
> first "for" loop and replace it with something else to do the same general
> function?

for key in keys:
  print 'Attribute %s has value %s' % (key, attrs[key])

-- 
John.


More information about the Tutor mailing list