# SymPy | Permutation.next_nonlex() in Python

Permutation.next_nonlex() : next_nonlex() is a sympy Python library function that returns the next permutation in non lexicographical order and if in case the self is the last permutation in non-lexicographical order it returns None.

Syntax : sympy.combinatorics.permutations.Permutation.next_nonlex()

Return : next permutation in non lexicographical order

Code #1 : next_nonlex() Example

 `# Python code explaining ` `# SymPy.Permutation.next_nonlex() ` ` `  `# importing SymPy libraries ` `from` `sympy.combinatorics.partitions ``import` `Partition ` `from` `sympy.combinatorics.permutations ``import` `Permutation ` ` `  `# Using from sympy.combinatorics.permutations.Permutation.next_nonlex() method  ` ` `  `# creating Permutation ` `a ``=` `Permutation([[``2``, ``0``], [``3``, ``1``]]) ` ` `  `b ``=` `Permutation([``1``, ``3``, ``5``, ``4``, ``2``, ``0``]) ` ` `  ` `  `print` `(``"Permutation a - next_nonlex form : "``, a.next_nonlex()) ` `print` `(``"Permutation b - next_nonlex form : "``, b.next_nonlex()) `

Output :

Permutation a – next_nonlex form : (0 3 2)
Permutation b – next_nonlex form : (0 5 1 3 4 2)

Code #2 : next_nonlex() Example – 2D Permutation

 `# Python code explaining ` `# SymPy.Permutation.next_nonlex() ` ` `  `# importing SymPy libraries ` `from` `sympy.combinatorics.partitions ``import` `Partition ` `from` `sympy.combinatorics.permutations ``import` `Permutation ` ` `  `# Using from  ` `# sympy.combinatorics.permutations.Permutation.next_nonlex() method  ` ` `  `# creating Permutation ` `a ``=` `Permutation([[``2``, ``4``, ``0``],  ` `                 ``[``3``, ``1``, ``2``], ` `                 ``[``1``, ``5``, ``6``]]) ` ` `  ` `  `print` `(``"Permutation a - next_nonlex form : "``, a.next_nonlex()) `

Output :

Permutation a – next_nonlex form : (0 3 5 1 6 2 4)

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.