Why use a tuple instead of a list?

I'd use a tuple for heterogenous and/or fixed aggregates. Otherwise I'd use a list. Of course, there are always exceptions, but that's a generally universal guideline.

Actually, tuples are mainly used by the Python language itself, for certain types of operations where lists would too slow, or where the immutability of tuples is helpful. For example, multiple return values are actually passed as a tuple, which can either be assigned as a single value or 'unpacked' across multiple variables.

Tuples, since they are immutable (can not be changed), are also useful for keys in a dictionary.

After a while you get used to using tuples where feasible. For instance, I've just written a program to generate an image with alternating yellow and pink (!) stripes. First lines of code were:

yellow = (244, 244,   0, 255)
pink   = (255, 142, 200, 255)

(for R,G,B,Alpha). Why choose tuples over lists? Well, these are read-only constants and it just seems natural to me to do it this way. Practically speaking, lists would work just as well, but I have an ingrained preference for the more efficient tuples -- where possible.

A tuple uses much less memory than a list.

And should be faster.

C:\>python -m timeit (1,2,3,4)
10000000 loops, best of 3: 0.0226 usec per loop

C:\>python -m timeit [1,2,3,4]
10000000 loops, best of 3: 0.194 usec per loop

Yes tuple are faster than list.

Interesting ...

E:\>python -m timeit (1,2,3,4)
10000000 loops, best of 3: 0.0173 usec per loop

E:\>python -m timeit [1,2,3,4]
10000000 loops, best of 3: 0.1710 usec per loop

~Same difference, factor of 10 here.

Edited 3 Years Ago by BearofNH: Fix claim

So how would speed test over look in PyPy?

C:\pypy>pypy -m timeit (1,2,3,4)
1000000000 loops, best of 3: 0.00102 usec per loop

C:\pypy>pypy -m timeit [1,2,3,4]
1000000000 loops, best of 3: 0.00102 usec per loop

Hmm faster and no time differnce,don't ask why PyPy is the diabolical work of super programmers.
A god a funny video when David Beazley look into PyPy.

Edited 3 Years Ago by snippsat

And then there is the named tuple that uses named indexing rather than sequential numeric indexing. It is supposed to be only slightly less efficient than a regular tuple.

Here is an example ...

''' namedtuple_record1.py
named tuples behave somewhat like a class
'''

import collections as co

# create the named tuple
Record = co.namedtuple('employee_record', 'name, department, salary')

# load the named tuple and create named instances
bob = Record('Bob Zimmer', 'finance', 77123)
tim = Record('Tim Bauer', 'shipping', 34231)

sf = "{} makes ${:,} annually"

# access by numeric index like a regular tuple
print(sf.format(tim[0], tim[2]))

# or access by named index (more readable)
print(sf.format(bob.name, bob.salary))

'''
Tim Bauer makes $34,231 annually
Bob Zimmer makes $77,123 annually
'''

Edited 3 Years Ago by vegaseat

Doesn't PyPy actually translate to C?

Yes that one of the process,if you look at video bye David Beazle you get a better overview.
There is a lot of heavy stuff under the cover.

Edited 3 Years Ago by snippsat

How do you run PyPy?

Run PyPy is very easy.
Windows download Windows binary (32bit)
Pack out to example C:\pypy
Navigate to folder in cmd then.
C:\pypy>pypy some_file_to_run.py

Linux example Mint,search software manager for PyPy install.
Navigate to folder.

tom@tom-VirtualBox:/usr/lib/pypy/bin > ./pypy-c -m timeit [1,2,3,4]
1000000000 loops, best of 3: 0.000777 usec per loop
tom@tom-VirtualBox:/usr/lib/pypy/bin > 

So pypy or pypy-c just replace python as run command.

For even better performance,look at video from 9 min.
There is possible to run translate.py -Ojit on your computer.

Edited 3 Years Ago by snippsat

The difference in memory size between a tuple and a list is not very large ...

import sys

mytuple = ()
mylist = []

print("Size of empty tuple = {}".format(sys.getsizeof(mytuple)))
print("Size of empty list = {}".format(sys.getsizeof(mylist)))

'''
Size of empty tuple = 28
Size of empty list = 36
'''
This question has already been answered. Start a new discussion instead.