Should you use a list or a tuple ... or perhaps a dictionary to store a collection of objects in your Python program? To help you make the decision, think whether you need to be able to modify the collection after you've created it (mutable) or not (immutable), and whether you want it to be indexed in order, or not.
Those two answers should lead to your collection type:
| | Unordered | Ordered |
|---|
| Mutable | Dictionary { and } | List [ and ] |
|---|
| Immutable | n/a | Tuple ( and ) |
|---|
If you want an immutable unordered collection (the empty box in my table), use a dictionary. And when you're initially creating the collection, use ( ), [ ] or { } as shown in the table. Always use [ and ] to reference member elements after the initial creation - Python does it like that to render any code you write polymorphic ... i.e. to be common code that will work whatever the collection type.
(written 2006-11-29 17:15:54)
Associated topics are indexed under
Y104 - Python - Lists and Tuples [2368] Python - fresh examples of all the fundamentals - (2009-08-20)
[2284] Strings as collections in Python - (2009-07-12)
[2280] Creating and iterating through Python lists - (2009-07-12)
[1789] Looking for a value in a list - Python - (2008-09-08)
[1641] Tektronix 4010 series / Python Tuples - (2008-05-13)
[1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
[899] Python - extend v append on a list - (2006-10-20)
[657] The ternary operator in Python - (2006-03-25)
[383] Overloading of operators on standard objects in Python - (2005-07-19)
Y107 - Python - Dictionaries [1145] Using a list of keys and a list of values to make a dictionary in Python - zip - (2007-04-13)
[1144] Python dictionary for quick look ups - (2007-04-12)
[103] Can't resist writing about Python - (2004-10-29)
Some other Articles
It's the 1st, not the 1nd 1rd or 1th.Plain Ole nice picturesImproving the historic town of MelkshamPython security - trouble with inputPython collections - mutable and imutableSplitting Pythons in BradfordChristmas in NovemberComing from London to Melksham by train for a courseWhat happened at GeekmasPython and the Magic Roundabout