mahmoud / boltons

🔩 Like builtins, but boltons. 250+ constructs, recipes, and snippets which extend (and rely on nothing but) the Python standard library. Nothing like Michael Bolton.
https://boltons.readthedocs.org
Other
6.53k stars 353 forks source link

itertools.product with configurable type #208

Open kurtbrose opened 5 years ago

kurtbrose commented 5 years ago

find myself wanting to do

itertools.product('ABC', '123')

and get a result like

['A1', 'A2', ... , 'C3']

however, itertools always gives you back iterables of tuples

mblahay commented 4 years ago

Here is a fairly simply way to accomplish what you desire:

list("".join(x) for x in itertools.product("ABC"', "123"))

You can also create a simply construct using map() to do the same

list(map(lambda x: ''.join(x),itertools.product('ABC', '123')))

mblahay commented 4 years ago

@kurtbrose, when you say configurable type, are you looking for a version of product that can accept a transformation function?

mblahay commented 4 years ago

@mahmoud , Should this be closed?

mahmoud commented 4 years ago

Hmm, @mblahay I'll probably leave it for the minute. I have a feeling @kurtbrose will come back one of these days with some clarification :)