Dear Reader of this post,
do you have an idea how to form the sequence of a union of two lists efficiently in Python? Suppose there are the two following lists:
list_a = [['a','b'],['a','c'],['b','c']]
list_b = ['h','i']
and I want to calculate:
new_list = [['a','b','h'],['a','b','i'],['a','c','h'],['a','c','i'],['b','c','h'],
['b','c','i']].
Up to now, I am using the following loops:
new_list = [r+[j] for j in list_b for r in list_a]
However, I find this ugly. I do not like the two loops. I prefer using functions instead.
Do you have another idea to achieve the desired task? (Among other things, I tried to follow this Get the cartesian product of a series of lists in Python suggestion, but to no avail.)
I am grateful for any suggestions!
Best regards, Fabian