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

Alex Martelli aleaxit at yahoo.com
Thu May 17 11:25:27 EDT 2001


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

For example:
    print '%d %d' % tuple(l)


> would like something more in the line of:
> ->> print '%d %d' %(l[i], for i in range(2))
> for longer lists.

    print '%d %d' % tuple(l[:2])

seems best to me.  But if you insist:

    print '%d %d' % tuple([l[i] for i in range[2]])

will also work (it just seems excessive
complication to use a list comprehension
IF all you want is a slice!-).


Alex






More information about the Python-list mailing list