I want to get a list of ('A_1','A_2','B_1','B_2','C_1','C_2')
, with something like '_'.join()
and [(x, y) for x in ['A','B','C'] for y in [1,2]]
.
How to write this?
I want to get a list of ('A_1','A_2','B_1','B_2','C_1','C_2')
, with something like '_'.join()
and [(x, y) for x in ['A','B','C'] for y in [1,2]]
.
How to write this?
Use itertools.product
:
On python3.6+
import itertools
letters = ['A','B','C']
nums = [1, 2]
result = [f"{l}_{n}" for l, n in itertools.product(letters, nums)]
Or in lower python versions:
result = ["{}_{}".format(l, n) for l, n in itertools.product(letters, nums)]
Output:
>>> result
['A_1', 'A_2', 'B_1', 'B_2', 'C_1', 'C_2']