[Tutor] questions about tuples

Eric Brunson brunson at brunson.com
Wed Sep 26 20:36:18 CEST 2007


Fangwen Lu wrote:
> Dear all-
>  
> I want to do some loops. Each loop will generate a tuple. Eventually I 
> want to put tuples together in a higher level of tuple. Do you know 
> how to do this?
>  
> Say a=(1,2,3), b=(4,3,2),c=(9,5,6). I want to get a tuple as ((1,2,3), 
> (4,3,2),(9,5,6)).
>  
> If there are just 2 tuples, I can write x=a and then x=(x,b). But for 
> 3 tuples or more, I will get something like (((1,2,3), (4,3,2)),(9,5,6)).
 >>> a=(1,2,3)
 >>> b=(4,3,2)
 >>> c=(9,5,6)
 >>> x = (a,b,c)
 >>> print x
((1, 2, 3), (4, 3, 2), (9, 5, 6))


Or:

 >>> x = []
 >>> x.append(a)
 >>> x.append(b)
 >>> x.append(c)
 >>> print x
[(1, 2, 3), (4, 3, 2), (9, 5, 6)]
 >>> x = tuple(x)
 >>> print x
((1, 2, 3), (4, 3, 2), (9, 5, 6))


You can't use append() on a tuple, because a tuple is, by design, immutable.
>  
> Thanks.
>  
> Fangwen
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>   



More information about the Tutor mailing list