Please provide a better explanation of tuples and dictionaries

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Jan 30 02:12:59 EST 2013


On Tue, 29 Jan 2013 22:14:42 -0800, Daniel W. Rouse Jr. wrote:

> "John Gordon" <gordon at panix.com> wrote in message
> news:keaa9v$1ru$1 at reader1.panix.com...

>> A tuple is a linear sequence of items, accessed via subscripts that
>> start at zero.
>>
>> Tuples are read-only; items cannot be added, removed, nor replaced.
>>
>> Items in a tuple need not be the same type.
>>
>> Example:
>>
>>    >>> my_tuple = (1, 5, 'hello', 9.9999)
>>    >>> print my_tuple[0]
>>    1
>>    >>> print my_tuple[2]
>>    hello
>>
> To me, this looks like an array. Is tuple just the Python name for an
> array?

Absolutely not. Arrays can be modified in place. Tuples cannot. Arrays 
can be resized (depending on the language). Tuples cannot. Arrays are 
normally limited to a single data type. Tuples are not.

Python lists are closer to arrays, although the "array" type found in the 
array module is even closer still.

You create lists either with the list() function, or list builder 
notation using [ ].

Tuples are intended to be the equivalent of a C struct or Pascal record. 
Lists are very roughly intended to be somewhat close to an array, 
although as I said the array.py module is even closer.


>> A dictionary is a mapping type; it allows you to access items via a
>> meaningful name (usually a string.)
[...]
> Thank you, I understand it better it is kind of like a hash table.

Correct. Also known as "associative array".


-- 
Steven



More information about the Python-list mailing list