Combining two lists in Python

I need a code that finds out the possible passwords in a universe of LLLDDD (L=letter, d=digit), I found it easier to apply the conditions separately and came to two lists:

lpossiveis:['ABD', 'ACD', 'ADB', 'ADC', 'ADE', 'ADF', 'AED', 'AFD', 'BAD', 'BDA', 'CAD', 'CDA', 'DAB', 'DAC', 'DAE', 'DAF', 'DBA', 'DCA', 'DEA', 'DFA', 'EAD', 'EDA', 'FAD', 'FDA']

dpossiveis: [314, 323, 332, 341, 611]

Now I need to create a third list between the combination of the two, where the first element of lpossiveis is concatenated with the first element of dpossiveis, the first element of lpossiveis is concatenated with the second element of dpossiveis and so on. The result should ser:

senhas: ['ABC314', 'ABC323', 'ABC332', 'ABC341', 'ABC611', 'ACD314', ...  ]

My code looks like this:

def senhaspossiveis(lpossiveis, dpossiveis):
    senhas = []
    for i in lpossiveis:
        for i in dpossiveis:
            senhas.append(lpossiveis[i], dpossiveis[i])
    return senhas

But it is not creating the third list.

Author: hkotsubo, 2020-08-14

3 answers

The problem is that the method append only gets one argument, but you are passing two: lpossiveis[i] and dpossiveis[i].

One solution would be to concatenate these values, as suggested in other answers.

But if it's not an exercise where you have to do everything manually, another option is to use the itertools module, which has the function product, that already creates these combinations for you.

The only detail is that it returns the combinations in tuples, so you have to join the elements of each tuple into a single string:

from itertools import product

lpossiveis = ['ABD', 'ACD', 'ADB', 'ADC', 'ADE', 'ADF', 'AED', 'AFD', 'BAD', 'BDA', 'CAD', 'CDA', 'DAB', 'DAC', 'DAE', 'DAF', 'DBA', 'DCA', 'DEA', 'DFA', 'EAD', 'EDA', 'FAD', 'FDA']
dpossiveis = [314, 323, 332, 341, 611]

senhas = [ f'{s}{num}' for s, num in product(lpossiveis, dpossiveis) ]

Or, if you need a function that does this:

from itertools import product

def senhas_possiveis(letras, numeros):
    return [ f'{s}{num}' for s, num in product(letras, numeros) ]

lpossiveis = ['ABD', 'ACD', 'ADB', 'ADC', 'ADE', 'ADF', 'AED', 'AFD', 'BAD', 'BDA', 'CAD', 'CDA', 'DAB', 'DAC', 'DAE', 'DAF', 'DBA', 'DCA', 'DEA', 'DFA', 'EAD', 'EDA', 'FAD', 'FDA']
dpossiveis = [314, 323, 332, 341, 611]
senhas = senhas_possiveis(lpossiveis, dpossiveis)

The detail is that the code above creates a list with all possible passwords, and depending on the size of the initial lists, it can get very large.

If you actually need a list with all the passwords, then leave it that way. But if you just need to go through the passwords (in a for, for example) and do something with them (and then it will not need them for nothing else), just use product directly in for:

for s, num in product(lpossiveis, dpossiveis):
    senha = f'{s}{num}'
    # fazer algo com a senha

The difference is that product returns an iterator that does not compute all passwords at once, but rather one at a time (one at each iteration of for). If the idea is just to do something with each password and then you will not need them for anything else, this is more efficient than creating a list with all the possibilities (and this approach becomes more interesting if the amount of possible passwords is too much large).

But if you need a list with all the passwords, there is no way, you have to create it yourself.

 3
Author: hkotsubo, 2020-08-14 19:30:28

Follows a possible solution to your problem:

def senhaspossiveis(lpossiveis, dpossiveis):
    senhas = []
    for letras in lpossiveis:
        for  numeros in dpossiveis:
            combinacaosenha = letras+str(numeros)
            senhas.append(combinacaosenha)
    return senhas

lpossiveis = ['ABD', 'ACD', 'ADB', 'ADC', 'ADE', 'ADF', 'AED', 'AFD', 'BAD', 'BDA', 'CAD', 'CDA', 'DAB', 'DAC', 'DAE', 'DAF', 'DBA', 'DCA', 'DEA', 'DFA', 'EAD', 'EDA', 'FAD', 'FDA']
spossiveis = [314, 323, 332, 341, 611]

saida = senhaspossiveis(lpossiveis, spossiveis)

print(saida)
 2
Author: Danizavtz, 2020-08-14 13:20:18

Hello, Thais. I made some changes to your code and I think it worked.

Code:

def senhaspossiveis(lpossiveis, dpossiveis):
    senhas = []
    for l in lpossiveis:
        for d in dpossiveis:
            senhas.append(l + str(d))
    return senhas

lpossiveis = ['ABD', 'ACD', 'ADB', 'ADC', 'ADE', 'ADF', 'AED', 'AFD', 'BAD', 'BDA', 'CAD', 'CDA', 'DAB', 'DAC', 'DAE', 'DAF', 'DBA', 'DCA', 'DEA', 'DFA', 'EAD', 'EDA', 'FAD', 'FDA']
dpossiveis = [314, 323, 332, 341, 611]

senhas = senhaspossiveis(lpossiveis, dpossiveis)

Note that the iterations that are taking place in the for are about the elements of the lists, and not about the index of the elements of the lists. Hence the concatenation is simple: l + str (d). The str () function turns the integers of dpossives into string.

Good Morning!!

 2
Author: Carlos Eduardo, 2020-08-14 13:26:40