Take the 2-minute tour ×

I'm doing some exploring of various languages I hadn't used before, using a simple Perl script as a basis for what I want to accomplish. I have a couple of versions of something, and I'm curious which is the preferred method when using Python -- or if neither is, what is?

Version 1:

workflowname = []
paramname = []
value = []
for line in lines:
        wfn, pn, v = line.split(",")

Version 2:

workflowname = []
paramname = []
value = []
i = 0;
for line in lines:
        workflowname[i], paramname[i], value[i] = line.split(",")
        i = i + 1

Personally, I prefer the second, but, as I said, I'm curious what someone who really knows Python would prefer.

share|improve this question
Please raise your accepting rate. Why would we care about your questions if you don't care about our answers. See stackoverflow.com/faq#howtoask and meta.stackoverflow.com/questions/5234/… –  JMax Feb 2 '12 at 14:44
The second one is less performant than the first. Also the first is more pythonic in my opinion, the latter is even unpythonic i think. –  Niklas R Feb 2 '12 at 14:46
In general the less lines the better –  pyCthon Feb 2 '12 at 14:46
They're all potentially terrible. Why are you separating these things that arrived together in the input file? –  S.Lott Feb 2 '12 at 14:46
In general, number of lines is irrelevant, what counts is how long it takes the reader to understand the code fully. If that takes more lines, so be it. First is better here on all scores. Second sucks hard. Loop counter? If you really want one of those (you don't) use enumerate. –  David Heffernan Feb 2 '12 at 14:48
add comment

4 Answers

up vote 2 down vote accepted

Bogdan's answer is best. In general, if you need a loop counter (which you don't in this case), you should use enumerate instead of incrementing a counter:

for index, value in enumerate(lines):
    # do something with the value and the index
share|improve this answer
Okay, yeah, that does everything I need. –  Sean Feb 2 '12 at 14:57
I mean, I wouldn't recommend using this code. @bogdan's is better in every way. Good luck anyway...! –  Mr E Feb 2 '12 at 15:01
... the enumerate() is totally unnecessary because the index isn't being used to do anything useful :( –  Matt Luongo Feb 2 '12 at 15:04
-1: Loop counters are not very Pythonic to begin with. They're rarely useful. This is a particularly bad piece of coding. While it works, it's bizarre. –  S.Lott Feb 2 '12 at 15:39
Right, I should just change the answer so that it points out the existence of enumerate, which is sometimes useful, and not present it as a solution to the problem here, which it was not intended to be. –  Mr E Feb 2 '12 at 16:11
add comment

A Pythonic solution might a bit like @Bogdan's, but using zip and argument unpacking

workflowname, paramname, value = zip(*[line.split(',') for line in lines])

If you're determined to use a for construct, though, the 1st is better.

share|improve this answer
add comment

Of your two attepts the 2nd one doesn't make any sense to me. Maybe in other languages it would. So from your two proposed approaces the 1st one is better.

Still I think the pythonic way would be something like Matt Luongo suggested.

share|improve this answer
I'd like that, but it doesn't allow me to save away the data for later parsing. And while in this particular case, I don't do a lot of work in the second pass, in other cases I do. Sometimes I need to run multiple passes through the data to get the exact information I need. –  Sean Feb 2 '12 at 14:56
Am I the only one who dropped this into an interpreter? This doesn't work... –  Matt Luongo Feb 2 '12 at 15:02
I don't really know what you mean by 'save away the data for later parsing'. I agree this can't be user in any situation but in your case and even in more complex situations it's still the 'pythonic' way to go imo. –  Bogdan Feb 2 '12 at 15:04
@Matt Luongo it sure works in mine TT. What input did you try for lines? Keep in mind that if OP question would work than you can assume an input of the form: lines = ['1,2,3', '4,5,6', '7,8,9'] –  Bogdan Feb 2 '12 at 15:06
Try it with 4 lines. Or 2. –  Matt Luongo Feb 2 '12 at 15:13
show 3 more comments

Version 1 is definitely better than version 2 (why put something in a list if you're just going to replace it?) but depending on what you're planning to do later, neither one may be a good idea. Parallel lists are almost never more convenient than lists of objects or tuples, so I'd consider:

# list of (workflow,paramname,value) tuples
items = []
for line in lines:
    items.append( line.split(",") ) 


class WorkflowItem(object):
    def __init__(self,workflow,paramname,value):
        self.workflow = workflow
        self.paramname = paramname
        self.value = value

# list of objects
items = []
for line in lines:
    items.append( WorkflowItem(*line.split(",")) ) 

(Also, nitpick: 4-space tabs are preferable to 8-space.)

share|improve this answer
add comment

Your Answer


