2

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.

stefano1
  • 161
  • 10
  • 4
    `tuple(id(x) for x in a)` does not create intermediate lists – vaultah Aug 10 '16 at 13:09
  • Thank, I did not know this possibility. But It seems slower, I edit my question. – stefano1 Aug 10 '16 at 13:15
  • Are you doing this in Python 2 or Python 3? – PM 2Ring Aug 10 '16 at 13:19
  • 2
    Not directly related to your question, but consider using the `timeit` module rather than `time.time` when comparing the run-time of code. `timeit` gives more reliable results because it runs each section multiple times, which lessens the impact of outside interference (such as Python going to sleep for a millisecond so the CPU can concentrate on a different application on your computer) – Kevin Aug 10 '16 at 13:19
  • On Python 3. @Kevin Thanks for the suggestion. – stefano1 Aug 10 '16 at 13:20

1 Answers1

5

Using the list comprehension is faster. This is because the size of the tuple is known beforehand (memory can be allocated for len(list) items).

With generator, the generator needs to be converted to a sequence first so that a tuple can be allocated. This will be slower because of the inherent overhead in generators.


The fastest option is naturally to not use a tuple at all and use a list instead. In my opinion, a tuple is not the same as "an immutable list"; one should really use tuple only when one needs to use a sequence as a dictionary key, or as a set element, or when you could name each element (as in namedtuple).