1

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

yosemite_k
  • 2,786
  • 1
  • 14
  • 26

2 Answers2

2

List comprehensions are a way to represent an expression. They cannot include statements. Use a regular loop.

for x in y:
    del df[x]
TigerhawkT3
  • 46,954
  • 6
  • 53
  • 87
0

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]
Illusionist
  • 4,754
  • 8
  • 41
  • 69