Project Euler # Triangular, pentagonal and hexagonal in Python Triangle, pentagonal, and hexagonal numbers are generated by the
  following formulae:
  
  
  Triangle: \$T_n=\frac{n(n+1)}{2}\$ 1, 3, 6, 10, 15, …
  Pentagonal: \$P_n=\frac{n(3n−1)}{2}\$ 1, 5, 12, 22, 35, …
  Hexagonal: \$H_n=n(2n−1)\$ 1, 6, 15, 28, 45, …
  
  
  It can be verified that \$T_{285} = P_{165} = H_{143} = 40755\$.
  
  Find the next triangle number that is also pentagonal and hexagonal.


from time import time


def tri_pent_hex(n):
    """yields triangles that are also pentagons and hexagons in range n."""
    triangles = set((num * (num + 1)) // 2 for num in range(1, n))
    pentagons = set((num * (3 * num - 1)) // 2 for num in range(1, n))
    hexagons = set(num * (2 * num - 1) for num in range(1, n))
    for triangle in triangles:
        if triangle in pentagons and triangle in hexagons:
            yield triangle


if __name__ == '__main__':
    start_time = time()
    print(max(tri_pent_hex(1000000)))
    print(f'Time: {time() - start_time} seconds.')
The way this was written, it was meant to steer you in the direction you did.


  Find the next triangle number that is also pentagonal and hexagonal.


You loop the triangles to find matches in the others:


for triangle in triangles:
    if triangle in pentagons and triangle in hexagons:
        yield triangle



But if you think about it, there are more triangles than pentagons and more pentagons than hexagons. And since you need to find occurences where all 3 types match, wouldn't it be better to loop the hexagons, and find a match on the others from there?

My point is: 

Don't let the way a question is asked influence the way you implement an algorithm.