I try to perform an inline operation when I need to sort a list as part of a process. The sort function of objects of type list works on the list on which it was called, instead of returning a result.
Python docs confirms the following:
list.sort ()
Sort list items in place.
I tried this through the Python command line and here is the result:
>>> a = list("hello").sort() >>> print a None >>> b = list("hello") >>> print b ['h', 'e', 'l', 'l', 'o'] >>> b.sort() >>> print b ['e', 'h', 'l', 'l', 'o']
Is there a way to skip this problem and make a line like the following possible?
result = list(random.choice(basesalts)).sort()
Using the code above will help me reduce the length and verbosity of my code.
source share