Python list sorting methods reverse, sort, sorted

Source: Internet
Author: User
Tags python list

There are three list sorting methods in the Python language: Reverse reverse/reverse order, sort positive order, and sorted to get a sorted list. In the more advanced list sort, the latter two methods can also be sorted by adding conditional parameters.

The reverse () method reverses the order of the elements in the list, such as: L=[4,2,3,1]l.reverse () print (l) # [1,3,2,4]reverse list reverse sort: The sequence of elements in the original list is re-deposited from left to right, The parameters in the list are not sorted. If you need to organize the parameters in the list, you need to sort the list in a different sort order. Sort () Sorting method This function method sorts the contents of the list forward, and the sorted new list overwrites the original list (the ID is the same), that is, the sort method is to directly modify the original list of lists sorting method. L=[4,2,3,1]l.sort () print (l) # [1,2,3,4] Many Python beginners, the sort () method is more confused. Sometimes you will need a sorted list, and you want to save the existing unordered lists, and they do the following: L=[4,2,3,1]l2=l.sort () print (L2) # None This time the problem arises, and variable B gets a null value. What if you want to get a sorted list and want to keep the original list? The list sorted () method can be implemented for you. The sorted () method preserves the original list and gets the sorted list sorted () operation as follows: L=[4,2,3,1]print (sorted (L)) # [1,2,3,4]sorted () method can be used in sequences of any data type, The return is always a list form: print (sorted (' Python ') # [' H ', ' n ', ' o ', ' P ', ' t ', ' y ') the difference between the sort () is a variable object (dictionary, list) method, no parameters, no return value, sort () will change the Mutable object , so no return value is required. The sort () method is a method or property unique to a Mutable object, and as immutable objects such as tuples, strings do not have these methods, and if called will return an exception. Sorted () is a python built-in function, not a unique method of a Mutable object (list, dictionary), and the sorted () function requires a parameter (the argument can be a list, a dictionary, a tuple, a string), and whatever parameter is passed, returns a return value in the list as a container. If it is, the dictionary returns a list of keys. Reverse () is the same as the use of sort, while reversed () and sorted () are used the same way through the sequence of slices can also achieve the effect of "reversal" L=[2,5,1,6,3,4]print (L[::-1]) # [4, 3, 6, 1, 5, 2 ]


Python list sorting methods reverse, sort, sorted

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.