-3

How do I do the following in the form of a oneliner:

replacements = set()
for i in replacement_per_file.values():
    replacements.update(i)
simonzack
  • 19,729
  • 13
  • 73
  • 118
Baz
  • 12,713
  • 38
  • 145
  • 268

4 Answers4

1

Something like this:

replacements = set(x for i in replacement_per_file.values() for x in i)
khelwood
  • 55,782
  • 14
  • 81
  • 108
0

If i is a list (or iterable), then you're looking for this:

replacements = set([
    x
    for i in replacement_per_file.values()
    for x in i
])
xecgr
  • 5,095
  • 3
  • 19
  • 28
0

An itertools version inspired from this post:

>>> from itertools import chain
>>> a = [[1, 2], [2, 3]]
>>> set(chain(*a))
set([1, 2, 3])
Community
  • 1
  • 1
Vincent
  • 12,919
  • 1
  • 42
  • 64
0

You can do this:

set().union(replacement_per_file.values())

This is slightly less efficient than your multi-line way, as it requires creating a copy of value references for the arguments of union.

simonzack
  • 19,729
  • 13
  • 73
  • 118