what is the difference between the two kinds of brackets?

Paul Hankin paul.hankin at gmail.com
Sat Oct 20 12:46:40 EDT 2007


On Oct 20, 11:15 am, "narutocan... at gmail.com" <narutocan... at gmail.com>
wrote:
> what is the difference between the two kinds of brackets?
> I tried a few examples but I can't make out any real difference:

The main difference in the language between tuples and lists is that
tuples (...) are immutable, and lists [...] are mutable.  That means
you should use lists rather than tuples if you need a mutable
structure (for example, if you want to extend it later), and use
tuples when you need an immutable structure (for example, if you're
constructing dict keys).

Perhaps my opinion is coloured by using other languages where the
distinction between list and tuple is stronger, but when I don't need
my data to be immutable or mutable, I use something like these rules
of thumb:
If each index has a particular meaning (for example (x, y) for
describing 2d coordinates), use tuples.
If the data is uniform (ie each element of the list/tuple has the same
meaning - for example, all the filenames in a directory), use lists.
If the data has a fixed length, use tuples.
If the data has an unknown - possibly unbounded - length, use lists.
If everything else is equal, use tuples.

HTH
--
Paul Hankin




More information about the Python-list mailing list