Hello I could not found the difference between using square brackets for comprehension list versus using list()
Is there a performance/ memory allocation difference ?
( same question for set and dict )
input = [1, 2, 3, 4]
B = [a * 2 for a in input if a > 1]
C = list(a * 2 for a in input if a > 1)
B_set = {str(a) for a in input if a > 1}
C_set = set(str(a) for a in input if a > 1)
B_dict = {str(a):a for a in input if a > 1}
C_dict = dict(str(a):b for a,b in input if a > 1) # NOT LEGAL
Thank you for your help
We can check with the
-mtimeit.So, as you can see, there is no considerable difference.
With bigger list, we have:
Where we see a 3 msec difference, better for the
[]case.With even bigger number list, we have
where we see a 0.28 sec difference, again
[]is faster.