List mapping?

Nick Trout nick at spam_me_notvideosystem.co.uk
Thu Apr 13 10:09:52 EDT 2000


Is there a nice way of pairing up the members to form tuple pairs in another
list? ie. like you might to processing input args.

ie. changing ['/a','1','/b','2','/c','3'] into [ ('/a','1'), ('/b','2'),
('/c','3') ]

eg:

args = '/a 1 /b 2 /c 3'
listargs= string.split(args)
tuplist = []
i=0
while i < len(listargs):
    tuplist.append( (listargs[i],listargs[i+1]) )
    i = i+2
for i in tuplist:
    processargs(i)

or:

args = '/a 1 /b 2 /c 3'
for i in string.split(args):
    i = argflag
    # i want next arg now as a parameter!!!

Can I (you!) get rid of the long winded while loop cleverly?!!  :-)

Thanks in advance,
Nick.








More information about the Python-list mailing list