Python - Tuples Conversions, methods, and immutability

Introduction

Tuples +, *, and slicing operations return new tuples.

To sort a tuple, convert it to a list to gain access to a sorting method call, or use the sorted built-in that accepts any sequence object.

Demo

T = ('cc', 'aa', 'dd', 'bb') 
tmp = list(T)                  # Make a list from a tuple's items 
tmp.sort()                     # Sort the list 
print( tmp )
T = tuple(tmp)                 # Make a tuple from the list's items 
print( T )
print( sorted(T) )             # Or use the sorted built-in, and save two steps
#  w w  w . java2  s  .co  m

Result

Here, the list and tuple built-in functions are used to convert the object to a list and then back to a tuple.