Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's wrong with this list comprehension (SyntaxError: invalid syntax)?

Having problems with what should be a "no brainer" LC.

Code snippet below:

def daterange(start_date, end_date):
    for n in range((end_date - start_date).days):
        yield start_date + dt.timedelta(n)


def get_workdays_between_dates(start_date, end_date):
    return [x in daterange(start_date, end_date) if x.date.weekday() in range(0,7)]

Python barfs a 'SyntaxError: invalid syntax error' when parsing function get_workdays_between_dates(). It looks ok to me though ...

What's wrong with the code?

like image 652
Homunculus Reticulli Avatar asked Jan 17 '23 11:01

Homunculus Reticulli


1 Answers

What's x? :) Yes, that's an invalid syntax, you are missing the for part.

[x for x in daterange(start_date, end_date) if x.date.weekday() in range(0,7)]
like image 115
Karoly Horvath Avatar answered May 03 '23 16:05

Karoly Horvath