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

Defining the argument of a python function to be n of the same lists

I have a python function itertools.product() and a list A = [1, 2, 3]. I would like to find a way to do itertools.product(A, A, A, ...) where the number of As in the argument can be determined by a variable n.

I tried itertools.product([A]*n), which obviously did not work. I expected this to not work, but I will reiterate that I would like to code something so that itertools.product([A]*n) => itertools.product(A, A, A, ...) where there are n As.

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

>Solution :

You can use argument unpacking using *: product(*[A]*n) to stick to your idea, but better use repeat option in that case if all A‘s in your list are to be the same: product(A, repeat=n).

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