Python provides list comprehensions that provide map/filter type functionality. Can I do a flatMap aka bind operation with this? I've seen solutions with itertools or other add-on libraries. Can I do this with core Python?
# this
[[x,10*x] for x in [1,2,3]]
# will result in unflattened [[1, 10], [2, 20], [3, 30]]
[y for x in [1, 2, 3] for y in [x, 10*x]]
Just add another for
to the list comprehension.
See python list comprehensions; compressing a list of lists?.
from functools import reduce
def flatMap(array: List[List]): List
return reduce(list.__add__, array)
# or return reduce(lambda a,b: a+b, array)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With