Here is an example of a set comprehension:
inp = [1,2,3,4,4,3,5]
s_comp = {element for element in inp}
# {1, 2, 3, 4, 5}
Does python know to convert the below into an immediate set comprehension (skipping over the initial list?) or does this first do a list comprehension and then convert the resulting list into a set (in other words, it's just a waste of resources using this notation to create a set).
s_comp = set([element for element in inp])
# {1, 2, 3, 4, 5}
Note, from cProfile it seems like it first does a list comprehension for the latter. Though each of them have the same amount of steps (I would imagine the first one should have one step less?).
>>> cProfile.run('{element for element in [1,2,3,4,4,3,5]}')
4 function calls in 0.000 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 <string>:1(<setcomp>)
1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
>>> cProfile.run('set([element for element in [1,2,3,4,4,3,5]])')
4 function calls in 0.000 seconds
Ordered by: standard name
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.000 0.000 0.000 0.000 <string>:1(<listcomp>) # list not set
1 0.000 0.000 0.000 0.000 <string>:1(<module>)
1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}