Questions about tuple?

Alex Martelli aleax at aleax.it
Sat Nov 9 02:32:29 EST 2002


Mindy wrote:
   ...
> In this example, actually what I want is to get
> 1,2,3,4,5 seperately from (1,2,3,4,5) and put them
> into five different lists, say, list1 =
> [1],list2=[2],list3=[3],list4=[4],list5=[5].
> 
> Is there any simply way to "split" a tuple? Thanks!

a, b, c, d, e = thetuple

will 'split' the tuple as you appear to request: i.e.,
just assign the tuple (which must have exactly five
items) to a target composed of five names.

When you get to as many as five things, this is not
a common tack; rather than keeping five separate list
variables, one would normally keep a list of five lists
and work by looping, e.g.:

listoflists = [ [] for i in range(5) ]

and then

for i in range(5):
    listoflists[i].append(thetuple[i])

but that's a choice of practicality and flexibility; if
you prefer to write things out five times, Python most
assuredly doesn't impede you from doing that.


Alex




More information about the Python-list mailing list