Outputting all possible permutations of multiple lists

Question:

I am very new to Python and have only just bought my first “Crashcourse in Python” book – originally my choice of language was PHP.

My Objective:

I desire a script that will output on-screen a list of all possible permutations of a particular pattern. Order is unimportant.

The raw data and pattern (the dataset will not change):

List1 = ['CA', 'CB', 'CC', 'CD', 'CE', 'CF', 'CG', 'CH', 'CJ', 'CK', 'CL', 'CM', 'CN', 'CO', 'CP', 'CR', 'CS', 'CT', 'CU', 'CV', 'CW', 'CX', 'CY']
List2 = ['51', '02', '52', '03', '53', '04', '54', '05', '55', '06', '56', '07', '57', '08', '58', '09', '59', '10', '60', '11', '61', '12', '62', '13', '63', '14', '64', '15', '65', '16', '66', '17']
List3 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
List4 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
List5 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

String Output:

[List1] + [List2] + [List3] + [List4] + [List5]

Example:

Resulting in lots of 7 character alphanumeric strings

  • CH07YCD
  • CT11MPP
  • etc.

Crap Maths:

Is my wonky maths correct in that I’d be looking at 10,174,464 entries? List1(23) x List2(32) x List3,4,5(13,824).

My question:

Is itertools the best function to use for this? If so, how? If not, what?

Asked By: Zander G

||

Answers:

>>> import itertools
>>> s = [List1, List2, List3, List4, List5]
>>> n = list(itertools.product(*s))
>>> len(n)
10174464

itertools.product -> Roughly equivalent to nested for-loops in a generator expression.

Answered By: Saket Mittal

To prove your math is indeed right:

List1 = ['CA', 'CB', 'CC', 'CD', 'CE', 'CF', 'CG', 'CH', 'CJ', 'CK', 'CL', 'CM', 'CN', 'CO', 'CP', 'CR', 'CS', 'CT', 'CU', 'CV', 'CW', 'CX', 'CY']
List2 = ['51', '02', '52', '03', '53', '04', '54', '05', '55', '06', '56', '07', '57', '08', '58', '09', '59', '10', '60', '11', '61', '12', '62', '13', '63', '14', '64', '15', '65', '16', '66', '17']
List3 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
List4 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
List5 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
print(len(List1)*len(List2)*len(List3)*len(List4)*len(List5))

Result:
10174464

You can also get the same result using loops:

i=0
List1 = ['CA', 'CB', 'CC', 'CD', 'CE', 'CF', 'CG', 'CH', 'CJ', 'CK', 'CL', 'CM', 'CN', 'CO', 'CP', 'CR', 'CS', 'CT', 'CU', 'CV', 'CW', 'CX', 'CY']
List2 = ['51', '02', '52', '03', '53', '04', '54', '05', '55', '06', '56', '07', '57', '08', '58', '09', '59', '10', '60', '11', '61', '12', '62', '13', '63', '14', '64', '15', '65', '16', '66', '17']
List3 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
List4 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
List5 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
for a in List1:
    for b in List2:
        for c in List3:
            for d in List4:
                for e in List5:
                    i+=1
print(i)

Result: 10174464

Therefore, your mathematics wasn’t wonky, it just is there are that many solutions.

Using loops to solve your problem:

i=0
List1 = ['CA', 'CB', 'CC', 'CD', 'CE', 'CF', 'CG', 'CH', 'CJ', 'CK', 'CL', 'CM', 'CN', 'CO', 'CP', 'CR', 'CS', 'CT', 'CU', 'CV', 'CW', 'CX', 'CY']
List2 = ['51', '02', '52', '03', '53', '04', '54', '05', '55', '06', '56', '07', '57', '08', '58', '09', '59', '10', '60', '11', '61', '12', '62', '13', '63', '14', '64', '15', '65', '16', '66', '17']
List3 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
List4 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
List5 = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
for a in List1:
    for b in List2:
        for c in List3:
            for d in List4:
                for e in List5:
                    print(f"{a}{b}{c}{d}{e}") #combines elements

For a more efficient way to do this, use itertools.

Answered By: Stephen Gzy