Remove sub-list from larger list

I looked at (
Removing sublist if matching a criteria) but still a bit confused
I can get the following to work in a command window:

>>> SelectedDates=['1/1/2023','1/2/2023','1/3/2023','1/4/2023','1/5/2023']
>>> workingSelections=['1/4/2023']
>>> res = [i for i in SelectedDates if i not in workingSelections]
>>> print(res)
['1/1/2023', '1/2/2023', '1/3/2023', '1/5/2023']

however, when run as a function in subline on my project as:

function clearOutMainList(){
  alert(SelectedDates);
  alert(workingSelections);
  var res = [i for i in SelectedDates if i not in workingSelections];
  alert(res);
}

It wont work

Is that javascript? Why would you expect a python list comprehension to work as-is in another language?

My mistake I confused what part of the app I was in

So are we.

What is “Removing sublist if matching a criteria” and why does it have parentheses around it? Is that your homework or something?

I don’t know Javascript very well, but this looks like it might work:

function clearOutMainList(){
    alert(SelectedDates);
    alert(workingSelections);
    var res = [i for i in SelectedDates if i not in workingSelections];
    alert(res);
}

if Javascript supports list comprehension syntax.

The Python equivalent would be:

  • change function to def
  • remove the braces { }
  • remove the semicolons
  • put a colon : at the end of the def ... line
  • change alert() to print()

but that is not very good programming style. But without knowing what your are supposed to do, it is hard to say what you should do instead.

P.S. in the future, “it won’t run” or “it doesn’t work” is a terrible way to ask a question. We’re not mind-readers and we probably won’t be able to guess what that means. If you get an error, please copy the entire error message, starting with the line “Traceback…” all the way to the end, and post it between code fences like this:

```
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type 'NoneType' has no len()
```