# Consultation on 2 lists in python and mathematics

4

I have 2 lists

``````lista1 = ["R1","R1","R2","R3","R4","R5","R6"]
lista2 = ["Frase 1","Frase 2","Frase 3","Frase 4","Frase 5","Frase 6"]
``````

I would like ALL the 'Rx' in the list1 to be combined with ALL of the list2. Not sequentially, but randomly. For example:

• R1 with Phrase 3
• R1 with Phrase 4
• etc

In the same way, the other R, but checking that no R would double their combination. If I have 6 Rs and 6 sentences the combination would be 6 x 6 = 36. Can someone help me please?

asked by papabomay 19.04.2016 в 17:50
source

5

If you want all the possible combinations, you can use the Cartesian product of both lists using `itertools.product` :

``````>>> import itertools
>>> lista1 = ["R1", "R2", "R3", "R4", "R5", "R6"]
>>> lista2 = ["Frase 1", "Frase 2", "Frase 3", "Frase 4", "Frase 5", "Frase 6"]
>>> combinaciones = list(itertools.product(lista1, lista2))
>>> len(combinaciones)
36
>>> combinaciones
[('R1', 'Frase 1'),
('R1', 'Frase 2'),
('R1', 'Frase 3'),
('R1', 'Frase 4'),
('R1', 'Frase 5'),
('R1', 'Frase 6'),
('R2', 'Frase 1'),
('R2', 'Frase 2'),
('R2', 'Frase 3'),
('R2', 'Frase 4'),
('R2', 'Frase 5'),
('R2', 'Frase 6'),
('R3', 'Frase 1'),
('R3', 'Frase 2'),
('R3', 'Frase 3'),
('R3', 'Frase 4'),
('R3', 'Frase 5'),
('R3', 'Frase 6'),
('R4', 'Frase 1'),
('R4', 'Frase 2'),
('R4', 'Frase 3'),
('R4', 'Frase 4'),
('R4', 'Frase 5'),
('R4', 'Frase 6'),
('R5', 'Frase 1'),
('R5', 'Frase 2'),
('R5', 'Frase 3'),
('R5', 'Frase 4'),
('R5', 'Frase 5'),
('R5', 'Frase 6'),
('R6', 'Frase 1'),
('R6', 'Frase 2'),
('R6', 'Frase 3'),
('R6', 'Frase 4'),
('R6', 'Frase 5'),
('R6', 'Frase 6')]
``````

If you want to choose a combination at random you can use `random.choice` :

``````>>> import random
>>> random.choice(combinaciones)
('R2', 'Frase 6')
>>> random.choice(combinaciones)
('R6', 'Frase 4')
>>> random.choice(combinaciones)
('R4', 'Frase 2')
``````

## Update

It is true that when using `random.choice` it is possible that at some point one will repeat. In that case, a solution may be to apply a `random.shuffle` and go consume the elements from the beginning to the end:

``````>>> random.shuffle(combinaciones)
>>> combinaciones
[('R6', 'Frase 4'),
('R4', 'Frase 3'),
('R3', 'Frase 6'),
('R5', 'Frase 2'),
('R2', 'Frase 1'),
('R4', 'Frase 5'),
('R5', 'Frase 5'),
('R4', 'Frase 4'),
('R1', 'Frase 2'),
('R1', 'Frase 3'),
('R6', 'Frase 2'),
('R3', 'Frase 2'),
('R5', 'Frase 3'),
('R6', 'Frase 5'),
('R6', 'Frase 1'),
('R3', 'Frase 4'),
('R5', 'Frase 6'),
('R2', 'Frase 3'),
('R2', 'Frase 4'),
('R4', 'Frase 6'),
('R2', 'Frase 5'),
('R1', 'Frase 4'),
('R3', 'Frase 3'),
('R4', 'Frase 2'),
('R4', 'Frase 1'),
('R1', 'Frase 1'),
('R1', 'Frase 5'),
('R3', 'Frase 1'),
('R6', 'Frase 6'),
('R2', 'Frase 6'),
('R1', 'Frase 6'),
('R6', 'Frase 3'),
('R2', 'Frase 2'),
('R5', 'Frase 1'),
('R5', 'Frase 4'),
('R3', 'Frase 5')]
``````

The variable `combinaciones` is a list of tuples , to access Rx and FraseX you only have to access the position `0` and `1` of each tuple. Considering the "shuffleada" list above:

``````>>> rx = combinaciones[0][0]
>>> rx
'R6'
>>> frase = combinaciones[0][1]
>>> frase
'Frase 4'
``````

``````>>> rx, frase = combinaciones[0]