1

I have a construct like the one below, how can I use 1 (or more) list comprehensions to get the same output??

f2remove = []
for path in (a,b):
    for item in os.listdir(path):
        if os.path.isdir(os.path.join(path,item)):
            x = parse_name(item)
            if x and (ref - x).days >= 0:
                f2remove.append(os.path.join(path,item))

I've tried multiple things like

files = [parse_name(item)\
         for item in os.listdir(path) \
         for path in (a,b)\
         if os.path.isdir(os.path.join(path,item))] # get name error
f2remove = [] # problem, don't have path...

Error:

Traceback (most recent call last):
  File "C:\Users\karuna\Desktop\test.py", line 33, in <module>
    for item in os.listdir(path) \
NameError: name 'path' is not defined
3
  • Shouldn't your if statement be tabbed in? Also what is your syntax error?
    – N_A
    Mar 11, 2013 at 18:13
  • Your comment says you're getting syntax error, while the traceback shows NameError. Mar 11, 2013 at 18:22
  • @AshwiniChaudhary My bad... was looking at a different script... (:P to me)
    – pradyunsg
    Mar 11, 2013 at 18:25

2 Answers 2

4

The order of fors doesn't change. The condition in your case becomes awkward:

f2remove = [
    os.path.join(path, item)
    for path in (a,b)
        for item in os.listdir(path)
            if os.path.isdir(os.path.join(path, item))
               for x in (parse_name(item),)
                  if x and (ref - x).days >= 0
   ]

basically, to convert nested fors to a list comprehension, you just move whatever you were appending to the front:

result = []
for a in A:
    for b in B:
      if test(a, b):
         result.append((a, b))

becomes

result = [
    (a, b)
    for a in A
    for b in B
    if test(a, b)
]
6
  • Is there any way of not calling parse_name() twice, it is a very slow function..
    – pradyunsg
    Mar 11, 2013 at 18:22
  • 1
    @Schoolboy - Yes. ... if os.path.isdir(os.path.join(path, item)) for x in (parse_name(item),) if x and (ref-x).days >= 0]
    – Robᵩ
    Mar 11, 2013 at 18:31
  • @Robᵩ Consider posting it as an answer I'll up-vote it (I'm going to accept this answer though)
    – pradyunsg
    Mar 11, 2013 at 18:33
  • Nah, My answer would be too derivative. @Pavel can incorporate what I said into his answer, though, if he cares to.
    – Robᵩ
    Mar 11, 2013 at 18:34
  • @Robᵩ When does an upvote get locked?? ( out of the thing that killed the cat (curiosity) )
    – pradyunsg
    Mar 11, 2013 at 18:36
2

This should do the job,

f2remove = [
       os.path.join(path,item) 
            for item in [os.listdir(path)
                for path in (a,b)] 
                     if os.path.isdir(os.path.join(path,item)) 
                           and parse_name(item) and 
                                 (ref - parse_name(item)).days >= 0]

but your initial version is really more readable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.