0

I am just now learning list comprehension in my python class and would like to know why the below code returns an invalid syntax error as a result of the "else" clause?

allDays=["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

daysWithPlaydate=[]

for days in range(len(allDays)):
    answer=input("Is there a playdate on " + allDays[days] + "?")
    if answer == "yes":
        daysWithPlaydate.append(allDays[days])
        
outputs=["Weekday Playdate: " + weekdays for weekdays in daysWithPlaydate if weekdays!="Friday" or weekdays!="Saturday" or weekdays!="Sunday" else "Weekend Playdate: " + weekdays for weekdays in daysWithPlaydate] 
for i in range(len(outputs)):
    print(outputs[i], end=", ")

P.S.: For the print statement at the end, I would like to know how to remove the final comma when I print out the list, as I always seem to get a comma after the final day in the list, thank you.

resstudent
  • 13
  • 2
  • Does this answer your question? [if/else in a list comprehension](https://stackoverflow.com/questions/4260280/if-else-in-a-list-comprehension) – Adam Strauss Mar 15 '21 at 06:28
  • you can make use of `join()` to achieve the desired format, please see my updated answer as I missed that earlier. – Krishna Chaurasia Mar 15 '21 at 07:11
  • Hi @conrorket, welcome to Stack Overflow! You could probably split the very long condition up a bit for readability by adding a function - something like `def not_in_weekend(day): return day != "Friday or ...`. Use that in the list comprehension. – Daniel Soutar Mar 15 '21 at 15:24

3 Answers3

2

You don't need to loop twice.

Also, the conditional statement is invalid and should be as:

outputs=["Weekday Playdate: " + weekdays if weekdays not in ["Friday", "Saturday", "Sunday"] else "Weekend Playdate: " + weekdays for weekdays in daysWithPlaydate] 

I have also made use of in keyword to further simplify the conditional statements.

To solve the output formatting issue, you can use the join() method to print the output as:

print(', '.join(outputs))

Output:

Weekday Playdate: Monday, Weekend Playdate: Saturday, Weekend Playdate: Sunday
Krishna Chaurasia
  • 8,924
  • 6
  • 22
  • 35
1

Valid Santax for list comprehension involve IF-ELSE is :

list_ = [statment1_  If cond1_ else statment2_ for i in iterable_]
Davinder Singh
  • 2,060
  • 2
  • 7
  • 22
  • I tried that but received an invalid syntax error at the "if" statement. – resstudent Mar 15 '21 at 06:29
  • Thanks for correcting me `[statment1_ (If cond1_ else statment2_) for i in iterable_]` should be `[statment1_ If cond1_ else statment2_ for i in iterable_]` where parentheses should not be used. – Davinder Singh Mar 15 '21 at 06:38
0

Krishna's answer should solve your list comprehension problem.

With regard to your question about comma at the end, if you want them in a line separated only by a comma, and no comma at the end, then you could just print the unpacked list, and use the seperator keyword instead of end for the print function. i.e.

print(*outputs, sep=", ")
ekken
  • 43
  • 6