Tuples comprehensions convert tuples.
The following, makes a list from a tuple, adding 20 to each item along the way:
T = (1, 2, 3, 4, 5) L = [x + 20 for x in T] print( L )
List comprehensions are sequence operations.
Tuples index and count methods:
T = (1, 2, 3, 2, 4, 2) # Tuple methods in 2.6, 3.0, and later print( T.index(2) ) # Offset of first appearance of 2 print( T.index(2, 2) ) # Offset of appearance after offset 2 print( T.count(2) ) # How many 2s are there? # www . j a v a 2 s . co m
Prior to 2.6 and 3.0, tuples have no methods.
A list inside a tuple, for instance, can be changed as usual:
T = (1, [2, 3], 4) #T = 'test' # This fails: can't change tuple itself #TypeError: object doesn't support item assignment # w w w. j a v a2 s. co m T = 'test' # This works: can change mutables inside print( T )