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

Yonas Kassa
  • 3,362
  • 1
  • 18
  • 27

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
  • 48,464
  • 6
  • 60
  • 97
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
  • 5,204
  • 11
  • 46
  • 76