1

In my current codebase, I have the following line where to_remove is a set

 [to_remove.update(b) for b in some_set if all(a <= b for a in some_dict)]

Although it works, it bothers me a little bit since it creates a list of None which is not used. Is it considered not standard? Is there a better way to do it?

Update:

Since it has been pointed out that list comprehension solely for side effect is not good, I have changed my code to

to_remove.update(itertools.chain.from_iterable(
    b for b in some_set if all(a <= b for a in some_dict))
Asclepius
  • 57,944
  • 17
  • 167
  • 143
nos
  • 19,875
  • 27
  • 98
  • 134

1 Answers1

2

It's not standard or recommended to use a list comprehension if its output is not assigned to a variable. A static analyzer such as pylint will even flag it.

Use a conventional loop instead:

for b in some_set:
    if all(a <= b for a in some_dict):
        to_remove.update(b)

In your specific case, since to_remove is a set, the following may or may not work correctly:

to_remove.update(b for b in some_set if all(a <= b for a in some_dict))
Asclepius
  • 57,944
  • 17
  • 167
  • 143
  • `to_remove.update(b for b in some_set if all(a <= b for a in some_dict))` does not give the same result – nos Feb 18 '17 at 02:34