![]() Integers are more straightforward in their comparison with the > operator: int_list = Other than the capital letters - the rest of the strings are sorted in ascending dictionary order! Sort List of Integers ![]() I has a lesser lexicographical value than blue, even though b should be before i in the dictionary, because capital letters always have a lesser lexicographic value than lowercase letters. The same logic is applied to the sorted() function: sorted_list = sorted(string_list) Strings are sorted lexicographically, when compared with the > operator: string_list = Strings are compared differently than integers, which are in turn, compared differently to custom objects, for instance. The way comparisons are done depends on the data type of the elements of the list. However, sorted() creates a copy of the list we supply, sorts the copy, and returns it - leaving the original intact: # Sorts copy of `my_list` and returns it The sorted() function works in much the same way as the sort() function does - and also accepts the same arguments. To sort in descending order, you can supply the reverse=True argument to the function: my_list.sort(reverse= True) # my_list is sorted in-place - the original list is changed
0 Comments
Leave a Reply. |