1

I try to run

 lst = ['ab','cd','cdc','cd','ab','abcd']
 g = set()
 list(map(lambda x:g.add(x),lst))

Then the set g contains correct elements

>>>print(g)
{'abcd', 'ab', 'cdc', 'cd'}

But if I remove the list conversion

lst = ['ab','cd','cdc','cd','ab','abcd']
g = set()
map(lambda x:g.add(x),lst)

Then g is empty

>>>print(g)
set()

Why does this happen

  • 1
    Unrelated to your actual question, but: rather than passing `g.add` to `map()`, consider simply doing `g = set(lst)`, or even `g = {'ab','cd','cdc','cd','ab','abcd'}`. – Kevin Oct 01 '18 at 19:32
  • 1
    The lambda expression is just an expensive wrapper around `g.add` that doesn't gain you anything. `map(g.add, lst)` works the same. – chepner Oct 01 '18 at 19:33
  • do **not** use `map` for side-effects – juanpa.arrivillaga Oct 01 '18 at 19:35

0 Answers0