0

python is very flexible and strong, but its syntax still a bit challanging for me.

I still have some difficulties to interpret some compact statement.

Below given one liner a bit confusing for me? How should I interpret this statement.

print list(set([x for x in outliers if outliers.count(x) >1]))
clockworks
  • 3,755
  • 5
  • 37
  • 46
  • `[x for x in outliers if outliers.count(x) >1]` is a list comprehension. `list(set(...))` is just normal syntax. – melpomene Sep 17 '17 at 10:05
  • 2
    This should be written as `[k for k, v in Counter(outliers).items() if v > 1]` anyway; see [the `Counter` documentation](https://docs.python.org/3/library/collections.html#collections.Counter) for its purpose. – Ry- Sep 17 '17 at 10:06
  • And `list(set([...]))` takes the resulting list, converts it to a set and back to a list, which removes duplicates at the cost of potentially changing the order. – jonrsharpe Sep 17 '17 at 10:06
  • It makes a new list out of a set. The set itself is made from the list comprehension `[x for x in outliers if outliers.count(x) >1]`, which is presumably the list of elements in outliers that appear in outliers more than once. The whole thing adds up to 'give me a list of elements that appear in outliers more than once'. – pvg Sep 17 '17 at 10:06

0 Answers0