Why converting list to set is faster than converting generator to set?
问题 Here is an example >>> from timeit import timeit >>> print(timeit('[y for y in range(100)]', number=100000)) 0.7025867114395824 >>> print(timeit('(y for y in range(100))', number=100000)) 0.09295392291478244 >>> print(timeit('set([y for y in range(100)])', number=100000)) 1.0864544935180334 >>> print(timeit('set((y for y in range(100)))', number=100000)) 1.1277489876506621 It is very confusing. Generator takes less time to create(and that is understandable) but why converting generator to set