Refer back to my other questions in Is there any other method to avoid duplicate result by looping inside a looping python (not groupby/transform)? i have solved using list