I have a list that contains 2 list:
[['JA@lazo.es', 'HI@lazo.es'], ['PO@jordi.es', 'GA@jordi.es'], '100', '1']
How I can combine all the possible combinations between the elements of the first list (position 0) and the second list (position 1) individually and at the same time create a new list with the elements that are not in those two positions of the original list.
So the result would be like:
[
['HI@lazo.es','PO@jordi.es','100', '1'],
['HI@lazo.es','GA@jordi.es','100', '1'],
['JA@lazo.es','PO@jordi.es','100', '1'],
['JA@lazo.es', 'GA@jordi.es','100', '1']
]
I can assume that the lists within the list will always be at position 0 and 1 but there could be multiple elements.
Thanks!
>Solution :
There are better ways to do this, but here is a simple way:
- Create an array for storing all the results, and iterate the first array
all_arrays = []
for a in items[0]:
- Iterate the second array
for b in items[1]:
- Create a new array consisting of
a,b, and the rest of your items; and append this array to the output
new_array = [a, b] + items[3:]
all_arrays.append(new_array)
For more reading on generating combinations of lists, I’d suggest checking out the itertools module