Lists and Tuples
Roy Smith
roy at panix.com
Fri Dec 5 13:11:18 EST 2003
In article <mailman.152.1070644249.16879.python-list at python.org>,
Skip Montanaro <skip at pobox.com> wrote:
> (Probably more response than you wanted. I kind of got carried away...)
>
> >> I've spent most of the day playing around with lists and tuples to
> >> get a really good grasp on what you can do with them. I am still left
> >> with a question and that is, when should you choose a list or a
> >> tuple?
>
> Generally, choose between tuples and lists based upon your data. In
> situations where you have a small, fixed collection of objects of possibly
> differing types, use a tuple. In situations where have a collection of
> objects of uniform type which might grow or shrink, use a list. For
> example, an address might best be represented as a tuple:
>
> itcs = ("2020 Ridge Avenue", "Evanston", "IL", "60201")
> caffe_lena = ("47 Phila Street", "Saratoga Springs", "NY", "12866")
>
> Though all elements are actually strings, they are conceptually different
> types.
That's a good example, because it makes for a nice segue. If you were
holding data from a form that looked like:
Street: ___________________________
City: ___________________________
State: ___________________________
Zip: ___________________________
Then I agree you're looking at a 4-tuple (assuming you didn't want to go
whole-hog and define an Address class). But, imagine a somewhat more
generic form that looked like:
Address line 1: ________________________
Address line 2: ________________________
Address line 3: ________________________
Address line 4: ________________________
You might fill in exactly the same data, but now if feels like it should
be a list.
More information about the Python-list
mailing list