TUPLE & LIST

Gerrit Holl gerrit at nl.linux.org
Thu Jan 15 09:18:09 EST 2004


Hi Yomanium Yoth Taripoät II (cool name!),

Yomanium Yoth Taripoät II wrote:
> 1) what are the differences between list and tuple?
> 2) how to concatenate tuple and list? no method, no opérator?
> 3) im looking the fucking manual, and cant add value in my tuple, when it
> already created :/
>   how to do it?

This question is answered in the FAQ:
http://www.python.org/doc/faq/general.html#why-are-there-separate-tuple-and-list-data-types

Lists and tuples, while similar in many respects, are generally used in
fundamentally different ways. Tuples can be thought of as being similar
to Pascal records or C structs; they're small collections of related
data which may be of different types which are operated on as a group.
For example, a Cartesian coordinate is appropriately represented as a
tuple of two or three numbers.

Lists, on the other hand, are more like arrays in other languages. They
tend to hold a varying number of objects all of which have the same type
and which are operated on one-by-one. For example, os.listdir('.')
returns a list of strings representing the files in the current
directory. Functions which operate on this output would generally not
break if you added another file or two to the directory.

Tuples are immutable, meaning that once a tuple has been created, you
can't replace any of its elements with a new value. Lists are mutable,
meaning that you can always change a list's elements. Only immutable
elements can be used as dictionary keys, and hence only tuples and not
lists can be used as keys.

yours,
Gerrit.

-- 
198. If he put out the eye of a freed man, or break the bone of a freed
man, he shall pay one gold mina.
          -- 1780 BC, Hammurabi, Code of Law
-- 
PrePEP: Builtin path type
    http://people.nl.linux.org/~gerrit/creaties/path/pep-xxxx.html
Asperger's Syndrome - a personal approach:
	http://people.nl.linux.org/~gerrit/english/




More information about the Python-list mailing list