As mentioned above, tuples are unchanged. Sorting a tuple (e.g. sorted(mytuple) ) returns a list that you will need to return back to the tuple.
To sort a tuple (and store it in a tuple), you will need to do this:
mytuple = (3,2,1) mysortedtuple = tuple(sorted(mytuple))
To sort the list, you will need to do this:
mylist = [3,2,1] mylist.sort()
Since you are not casting and re-casting, the latter is more efficient in this case.
Don’t get hung up on using tuples over lists unless you have a good excuse. If you need sorted data, tuples are not suitable if they are not created in this way in the first place. Tuples are superior when the data they contain DOES NOT CHANGE, for example, with configuration settings that are loaded at run time, or with data that has already been processed.
Given that you mentioned that you are processing a large data set, you may need to use a functional programming style with generators and iterators over lists and tuples. Thus, you do not move around and create new containers, but simply bind iteration operations to achieve the final result.
Further reading:
jathanism
source share