Examples of sorting methods for dictionary (dict) and list (list) in Python

Source: Internet
Author: User
This article mainly introduces the sorting methods of the dictionary (dict) and list (list) in Python. In summary, the built-in sort () method is preferred for sorting. For more information, see I. sort the list

The recommended sorting method is the built-in sort () method, which is the fastest and stable.

The code is as follows:


>>> A = [1, 9, 3, 7, 2, 0, 5]
>>> A. sort ()
>>> Print
[0, 1, 2, 3, 5, 7, 9]
>>> A. sort (reverse = True)
>>> Print
[9, 7, 5, 3, 2, 1, 0]
>>> B = ['e', 'A', 'be', 'AD', 'dab', 'ddb']
>>> B. sort ()
>>> Print B
['A', 'AD', 'be', 'dab', 'ddb', 'E']


The sorting of the list follows the DSU (decorate-sort-undecorate) mode, and the sequence is compared with the order of installation items. for the string in the preceding example, it is to compare characters one by one in the order from left to right. once the result is obtained, the comparison is stopped.

2. sort the dictionary (dict)

In fact, the dictionary (dict) is an unordered sequence. we can only sort the dictionary key/value, and then put the corresponding value/key in the same order.
Any sort of the dictionary will eventually be attributed to the sorting of the list consisting of the key or value of the dictionary (dict ).

1. sort by the dictionary key [1]

The code is as follows:


Def sortedDictValues (adict, reverse = False ):
Keys = adict. keys ()
Keys. sort (reverse = reverse)
Return [adict [key] for key in keys]


If you need to return the key and value at the same time, change the last return statement:

The code is as follows:

Return [(key, adict [key]) for key in keys]


There is also a simple writing method, that is, using the built-in sorted () method for sorting:

The code is as follows:


>>> D = {'C': 1, 'E': '5', 'B': 7}
>>> Sorted (d. items ())
[('B', 7), ('C', 1), ('e', '5')]


However, the performance may be slightly reduced. if the performance is demanding, it is better to use the native method for list. sort ().

2. Sort by dictionary value [2]

The code is as follows:


Def sorted_dict (container, keys, reverse ):
"Back to the keys list, sorted by the corresponding value in the INER """
Aux = [(container [k], k) for k in keys]
Aux. sort ()
If reverse: aux. reverse ()
Return [k for v, k in aux]


You can also use the sorted () method to implement the same function:

The code is as follows:

Sorted (d. items (), key = lambda d: d [1], reverse = True)

III. Conclusion

Through the analysis of the above code, we will summarize the following principles:
* The sorting of the dictionary is ultimately attributed to the sorting of the list composed of the dictionary keys or values.
* Sort the list by using the built-in list. sort () method.

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: info-contact@alibabacloud.com 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.