() vs []

Nanjundi nanjundi at gmail.com
Wed Oct 14 13:21:27 EDT 2009


On Oct 14, 1:05 pm, mattia <ger... at gmail.com> wrote:
> Any particular difference in using for a simple collection of element ()
> over [] or vice-versa?
>
> Thanks, Mattia

From: http://www.faqs.org/docs/diveintopython/odbchelper_tuple.html

1  	You can’t add elements to a tuple. Tuples have no append or extend
method.
2 	You can’t remove elements from a tuple. Tuples have no remove or
pop method.
3 	You can’t find elements in a tuple. Tuples have no index method.
4 	You can, however, use in to see if an element exists in the tuple.

So what are tuples good for?

    * Tuples are faster than lists. If you’re defining a constant set
of values and all you’re ever going to do with it is iterate through
it, use a tuple instead of a list.
    * Remember I said that dictionary keys can be integers, strings,
and “a few other types”? Tuples are one of those types. Tuples can be
used as keys in a dictionary, but lists can’t.[2]
    * Tuples are used in string formatting, as we’ll see shortly.




More information about the Python-list mailing list