Equivalent 2->> print '%d %d' %(l[i], for i in range(2))?

Carel Fellinger cfelling at iae.nl
Thu May 17 18:04:30 EDT 2001


Jorn Verwey <jorn.verwey at psi.ch> wrote:
> How can I avoid typing %(l[0],l[1]) in the following case?
> ->> l=[4,14]
> ->> print '%d %d' %(l[0],l[1])

Others have shown you proper ways of doing this, so all that's left
for me is some contrived code:

>>> class C:
...    def __init__(self, lst):
...      self.lst = lst
...    def __getitem__(self,key):
...      return self.lst[eval(key)]
...
>>> l=[1,2,3,4,5,6,7,8,9]
>>> "%(1)d %(2+3)d" % C(l)
'2 6'

-- 
groetjes, carel



More information about the Python-list mailing list