I have seen list comprehensions used, but have not quite got the hang of it
yet.
So, I was writing a bit of code to do some work with file directories and
decided
to give it a try as follows:

list_c = os.listdir("c:")

#first code written in the way I usually would.
dirs = []
for x in list_c:
    if os.path.isdir(x):
        dirs.append(x)

#replaced the above code with the following list comprehension, it worked as
expected:
dirs = [x for x in list_c if os.path.isdir(x)]

I can now see that quite a bit of the code I write dealing with lists can be
done with list
comprehensions.   My question is this, is the list comprehension styled code
generally
more efficient at runtime?  If so, why?

--Bill
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to