I need to create in the fastest way possible a Python tuple from a list, but with intermediate computations. To be clearer I put a code snippet:
a=[1,2,3,4]
b=tuple([id(x) for x in a])
This is what I have by now. Is there something better than this? This code creates a list and then it converts it to a tuple, is there a way to directly create the tuple?
Thank you!
[EDIT] As suggested I tried with
b=tuple(id(x) for x in a)
However it seems slower than before. Test script:
import time
a=[1,2,3,4]
t1=time.time()
for i in range(1000000):
b=tuple([id(x) for x in a])
print(time.time()-t1)
t2=time.time()
for i in range(1000000):
b=tuple(id(x) for x in a)
print(time.time()-t2)
It was very unexpected to me.