[Tutor] creating a list of data . . .

Don Arnold darnold02 at sprynet.com
Mon Aug 25 21:52:20 EDT 2003


----- Original Message -----
From: <Futurug at aol.com>
To: <tutor at python.org>
Sent: Monday, August 25, 2003 8:00 PM
Subject: [Tutor] creating a list of data . . .


> Thank you for
>
> I am absolutely new to programming with python, and I am learning quickly
>
> I am trying to create a tuple with 14 data in.
>
> my current code:
>
>     frug = frug , rug
>
> where frug is the final tuple and rug is the data I am trying to add
>
> I curretly get all 14 fields of data, but it only lets me call up
>
> I do not want to create one long string of data as the length of the
> individual data fields can vary.
>
> Thanks
>
> Kenny Sodbuster

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

someTuple = ()

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

print someTuple

>>> (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

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:

someList = []

for i in range(10):
    someList.append(i)

someOtherTuple = tuple(someList)

print someOtherTuple

>>> (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)


HTH,
Don




More information about the Tutor mailing list