Why does argparse return None instead of [] if an append action isn't used?

Ian Kelly ian.g.kelly at gmail.com
Mon Jan 26 19:23:34 CET 2015


On Mon, Jan 26, 2015 at 10:18 AM, Mark Lawrence <breamoreboy at yahoo.co.uk> wrote:
> IIRC, and probably from this list, creating tuples is way faster than
> creating lists, but accessing items is slower.  Can anybody confirm this for
> us?

The first seems to be true as long as the tuples are small.

$ python3 -m timeit 't = (1000, 2000, 3000)'
100000000 loops, best of 3: 0.0147 usec per loop
$ python3 -m timeit 't = [1000, 2000, 3000]'
10000000 loops, best of 3: 0.0678 usec per loop
$ python3 -m timeit 't = tuple(range(10000))'
10000 loops, best of 3: 183 usec per loop
$ python3 -m timeit 't = list(range(10000))'
10000 loops, best of 3: 174 usec per loop
$ python3 -m timeit 't = tuple(range(10000000))'
10 loops, best of 3: 323 msec per loop
$ python3 -m timeit 't = list(range(10000000))'
10 loops, best of 3: 306 msec per loop

This is probably a result of the use of freelists to avoid
reallocating the tuple objects, though. I don't see any substantial
difference in access time:

$ python3 -m timeit -s 't = tuple(range(10000))' 't[5000]'
10000000 loops, best of 3: 0.0316 usec per loop
$ python3 -m timeit -s 't = list(range(10000))' 't[5000]'
10000000 loops, best of 3: 0.0318 usec per loop
$ python3 -m timeit -s 't = tuple(range(10000))' 'for x in t: pass'
10000 loops, best of 3: 112 usec per loop
$ python3 -m timeit -s 't = list(range(10000))' 'for x in t: pass'
10000 loops, best of 3: 113 usec per loop



More information about the Python-list mailing list