What you propose to go for lista1 and for every value search lista2 is not very efficient, as you will be traveling lista2 complete by each element of lista1. The total number of iterations would be N*M, being N size lista1 and M size lista2.I propose another solution. Let's go. lista1 + lista2 and we're updating a dictionary in which each key is the identifier (assault of the element) and the value is a list, which will contain all the tuples in which that is the identifier. The number of iterations on this occasion would be N+M.I mean:diccionario = {}
for dato in lista1+lista2:
id = dato[1]
if id not in diccionario:
diccionario[id] = []
diccionario[id].append(dato)
At the end you will have a dictionary that has "grouped" in the same key the values that you have found on both lists. In this case it comes out:{'005789': [('1', '005789', 'A', 'LM3P'),
('1', '005789', 'C', 'NON'),
('52061', '005789', '190233', '19:04:18', 'AA', 'O', 'MD_NA')],
'005790': [('2', '005790', 'B', 'LM4P'),
('52063', '005790', '190233', '16:05:13', 'CC', 'O', 'MD_NA')]}
Then you can process it as you want, for example to generate the "mixture" list you needed (for this there will be another loop with K iterations, being K the size of the dictionary that would be less than N+M). I'm not very clear how you want to mix, so at the moment I don't do this part.