-2

The contents of the ScheduledStartDate list is either None or a string. How can I change my codes below to detect and skip the ScheduledStartDate[j] that contains None? And only run the statements when it is not None.

j = 0
while j< len1:
    if (ScheduledStartDate[j] == None):

    else:
        ScheduledStartDate[j] = datetime.datetime.strptime(ScheduledStartDate[j], format)
        
    j=j+1
Don99
  • 209
  • 1
  • 2
  • 9

1 Answers1

2

To my understanding this looks like it should work, however you may want to remove the else statement and just change the logic of your if statement.

j = 0
while j< len1:
    if ScheduledStartDate[j] is not None:
        ScheduledStartDate[j] = datetime.datetime.strptime(ScheduledStartDate[j], format)
        
    j=j+1

If your objective is to actually remove the None values then you can see how it's done here. Otherwise there is no way to know which values are None and which are strings without going through the entire list.

Also on another note, if you'd like your code to have that nice color scheme you see in all other questions, add the python tag to the question. Makes your code easier to read.

osacognitive
  • 103
  • 7
  • 2
    `if` is not a function and singletons like `None` should be compared with `is` or `is not` so it should be `if ScheduledStartDate[j] is not None:`. – Matthias Jul 19 '21 at 15:23