Nested for loop in Python to get equivalent result

welcome to shbcf.ru