[Baypiggies] Another Python koan: modifying a list

Casey Callendrello c1 at caseyc.net
Wed Nov 16 04:35:32 CET 2011

Howdy there,
Another python puzzle for everyone. I've been trying to come up with a 
Pythonic (i.e. no index variable) way of solving this.

I have a list consisting of one or more independent arguments to a 
specific command-line option. Think ./foo.py -a 1 -a 15 -a 1203. 
Argparse handles this fine. However, a few of my users prefer to specify 
arguments separated by commas instead. This is a fairly reasonable on 
their part, as about half of the tools we use like it one way, and half 
prefer the other. I don't mind supporting both, and the input data can 
never have a comma otherwise.

So, is there another way to express this code? In reality, the number of 
arguments is very small, so duplicating the array is no big deal. I'm 
still interested in a cleaner solution :-)

i = 0
while i < len(mylist):
     items = mylist[i].split(',')
     if len(items) > 1:
         x[i:i+1] = items

--Casey Callendrello

More information about the Baypiggies mailing list