-1

I have two lists lst and count I need enumerate on lst and check if lst elements exists in count or not if not then append it in another list else pass.

for i, u in enumerate(lst ):
    if u in count:
        pass
    else:
        _ = sku[i].pop("_id")
        objects.append(sku[i])
I need this code to be in one line
  • Can you show what you have tried so far? – Ethan Van den Bleeken Jun 15 '22 at 11:59
  • `_ = sku[i].pop("_id")` could be `del sku[i]["_id"]`, avoiding unused var. A one-liner may be possible but I don't think you can use a comprehension for this. Does `objects` start out empty i.e. this code is generating it, or is this appending to existing content? – Anentropic Jun 15 '22 at 12:04
  • 1
    Why do you "need" this in one line? – Kaz Jun 15 '22 at 12:05

1 Answers1

1

What's the content of lst, count, sku and objects before this code?

If I assume that these data are: list, list, list of dicts and empty list, you can maybe do something like this:

objects = [{k: v for k, v in sku[i].items() if k != i} for i, u in enumerate(lst) if u not in count]

It's one line, but it's not readable nor understandable!

I don't recommend you using one-line or comprehension list for such complex method.

Kaz
  • 1,047
  • 8
  • 18