Search code examples
pythonfilterproductpython-itertools

Filter works strangely together with product


So, i have this code:

from itertools import product

data = []
for _ in range(1, 3):
    for elm in product('01', repeat=_):
        data += [''.join(elm)]

print(data)

And Output:

['0', '1', '00', '01', '10', '11']

And this one:

from itertools import product

data = []
for _ in range(1, 3):
    data += filter(lambda elm: ''.join(elm), product('01', repeat=_))

print(data)

With output:

[('0',), ('1',), ('0', '0'), ('0', '1'), ('1', '0'), ('1', '1')]

The programs do the same things, but for some reason the version with filter gets a strange result. Why is that?


Solution

  • Sorry, guys. I mixed up filter and map. Worked hard, it happens. Thanks to Barmar a lot. Just replace filter to map...