-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
  • 17,855
  • 12
  • 65
  • 107
Baz
  • 11,687
  • 35
  • 137
  • 244

4 Answers4

1

Something like this:

replacements = set(x for i in replacement_per_file.values() for x in i)
khelwood
  • 52,115
  • 13
  • 74
  • 94
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
  • 4,753
  • 3
  • 16
  • 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,045
  • 1
  • 36
  • 58
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
  • 17,855
  • 12
  • 65
  • 107