How do i use a python keyword with a list comprehension, for example a del keyword in the following list comprehension.
[del df[x] for x in y]
thanks
How do i use a python keyword with a list comprehension, for example a del keyword in the following list comprehension.
[del df[x] for x in y]
thanks
List comprehensions are a way to represent an expression. They cannot include statements. Use a regular loop.
for x in y:
del df[x]
I am guessing you want to delete using list comprehension. Here's how you can do that-
df = [x for x in df if x not in to_remove]
Here's an example
>>> df =[1,2,3]
>>> to_remove=[1,2]
>>> df = [x for x in df if x not in to_remove]
>>> df
[3]