Results 1 to 6 of 6

Thread: Python: List vs Tuple?

  1. #1
    Join Date
    Apr 2007
    Location
    Lower 48, US
    Beans
    511
    Distro
    Ubuntu 11.04 Natty Narwhal

    Question Python: List vs Tuple?

    Let's assume I simply need a list of values to use in a for loop.

    In terms of speed: List or Tuple?

  2. #2
    Join Date
    May 2007
    Location
    East Yorkshire, England
    Beans
    Hidden!

    Re: Python: List vs Tuple?

    Code:
    barrucadu on randolph in ~/tmp [branch: master]
     >>>  uname -a; python -V
    Linux randolph 2.6.39-ARCH #1 SMP PREEMPT Fri May 20 11:33:59 CEST 2011 x86_64 Intel(R) Atom(TM) CPU N450 @ 1.66GHz GenuineIntel GNU/Linux
    Python 3.2
    Code:
    barrucadu on randolph in ~/tmp [branch: master]
     >>>  cat test-list.py; time python test-list.py; time python test-list.py; time python test-list.py
    foo = list (range (0, 1000000))
    
    for i in foo:
        i * 2
    Real: 1.22s User: 1.10s System: 0.12s Percent: 99% Cmd: python test-list.py
    Real: 1.28s User: 1.15s System: 0.10s Percent: 97% Cmd: python test-list.py
    Real: 1.23s User: 1.10s System: 0.11s Percent: 98% Cmd: python test-list.py
    Code:
    barrucadu on randolph in ~/tmp [branch: master]
     >>>  cat test-tuple.py; time python test-tuple.py; time python test-tuple.py; time python test-tuple.py
    foo = tuple (range (0, 1000000))
    
    for i in foo:
        i * 2
    Real: 1.23s User: 1.14s System: 0.09s Percent: 99% Cmd: python test-tuple.py
    Real: 1.27s User: 1.18s System: 0.09s Percent: 99% Cmd: python test-tuple.py
    Real: 1.26s User: 1.16s System: 0.09s Percent: 98% Cmd: python test-tuple.py
    Doesn't seem to be much difference.
    Website | Blog | The Arch Hurd Project

    If you want to ask about something I posted, send a PM, as I don't watch many threads

  3. #3
    Join Date
    May 2010
    Beans
    166

    Re: Python: List vs Tuple?

    Well, it should never come to do speed, it should come down to the fact of whether or not you need a mutable group of values. If you need speed so much that you would consider using different object types, then perhaps Python isn't the language for you.

    Anyway, the difference is beyond minute - and it depends on many many things, so it's hard to give you a solid answer. The major difference is the memory usage. Tuples will use less memory.

  4. #4
    Join Date
    Feb 2009
    Beans
    789
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Python: List vs Tuple?

    Rewrite that code in C. That's the fastest you can do.

    At first I assumed tuple would be faster as they are immutable but as the above profiling has shown, there's not much difference. That also makes sense actually because when both data types are constructed, the differences in bookkeeping and mutability don't really matter.
    "The eagle never lost so much time as when he submitted to learn from the crow."

  5. #5
    Join Date
    May 2007
    Location
    East Yorkshire, England
    Beans
    Hidden!

    Re: Python: List vs Tuple?

    I haven't confirmed this, but I wouldn't at all be surprised if internally tuples and lists were implemented using the same data structures and algorithms.
    Website | Blog | The Arch Hurd Project

    If you want to ask about something I posted, send a PM, as I don't watch many threads

  6. #6
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: Python: List vs Tuple?

    The only difference there might be is that lists, being mutable, won't be optimized as tuples could instead. Tuples are guarranteed to never change, so you can assume that in order to optimize code; you just can't assume that for lists, or at least not in the same extent that you can for tuples.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •