Reputation: 389
my_set = {1,2,3,4}
my_list = list(my_set)
Is this going to take O(n) or O(1) ?
Upvotes: 2
Views: 328
Reputation: 1303
According to the below results, the line my_list = list(my_set)
is clearly O(n).
my_set = {1,2}
%timeit my_list = list(my_set)
my_set = {1,2,3,4,5,6,7,8,9,10}
%timeit my_list = list(my_set)
my_set = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}
%timeit my_list = list(my_set)
output:
403 ns ± 5.07 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
621 ns ± 77.6 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
905 ns ± 27.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Upvotes: 2