[Tutor] creating a list of data . . .

Alan Gauld alan.gauld at blueyonder.co.uk
Wed Aug 27 21:56:02 EDT 2003


> As Kurt has pointed out, tuples are immutable: once they're created,
they
> can't be modified. They can, however, be rebound:

And just to make it clear what rebvound means in practice:

> someTuple = ()

The above creates a reference to an empty tuple.

> for i in range(10):
>     someTuple = someTuple + (i,)

Inside the loop we then create 2 new tuples.
The first is (i,) a single element one, which is
added to the existing tuple to create the second
new tuple comprising the combined data.

Thus in the above example we create a total of 21 tuples!

> Lists, on the other hand,  _are_ mutable, so it's often cleaner
> (and more efficient) to build a list and then convert it to
> a tuple when you're done:

The efficiency coming from the fact that you only have 1 list
which we manipulate instead of creating 21 distinct tuples!

Alan G.




More information about the Tutor mailing list