# Python | SymPy Partition.sort_key() method

Partition.sort_key() : sort_key() is a sympy Python library function that returns canonical key which can be used for string. This ordering of canonical key is based on the size and the elements of the partition whose ties are broken with the rank.

Syntax : sympy.combinatorics.partitions.Partition.sort_key()

Return : Conoanical key which can be used for string

Code #1 : sort_key() Example

 `# Python code explaining ` `# SymPy.sort_key() ` ` `  `# importing SymPy libraries ` ` `  `from` `sympy.utilities.iterables ``import` `default_sort_key ` `from` `sympy.combinatorics.partitions ``import` `Partition ` `from` `sympy.abc ``import` `x ` ` `  `# Using from sympy.combinatorics.partitions.Partition.sort_key() method  ` `g ``=` `Partition([``134``, ``322``]) ` `e ``=` `Partition(``list``(``range``(``3``))) ` `k ``=` `Partition([``44``, x]) ` `s ``=` `Partition([``3``, ``4``]) ` ` `  `lst ``=` `[s, k, g ``+` `1``, k, e] ` `lst.sort(key ``=` `default_sort_key); lst `

Output :

[{{3, 4}}, {{44, x}}, {{44, x}}, {{134}, {322}}, {{0, 1, 2}}]

Code #2 : sort_key() Example

 `# Python code explaining ` `# SymPy.sort_key() ` ` `  `# importing SymPy libraries ` ` `  `from` `sympy.utilities.iterables ``import` `default_sort_key ` `from` `sympy.combinatorics.partitions ``import` `Partition ` `from` `sympy.abc ``import` `x, y ` ` `  `# Using from sympy.combinatorics.partitions.Partition.sort_key() method  ` ` `  `k ``=` `Partition([``44``, x, y]) ` `s ``=` `Partition([``3``, ``55``, ``12``, ``4``]) ` ` `  `lst ``=` `[k, s] ` `lst.sort(key ``=` `default_sort_key); lst `

Output :

[{{44, x, y}}, {{3, 4, 12, 55}}]

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.