Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

List of possible combinations of elements within a list of list

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[ 
['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:

  1. Create an array for storing all the results, and iterate the first array
all_arrays = []
for a in items[0]:
  1. Iterate the second array
    for b in items[1]:
  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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading