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? # from w w w . j av a 2 s . c o 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 # from ww w.j a v a2 s . com T = 'test' # This works: can change mutables inside print( T )