AppDividend
Latest Code Tutorials

Python Sorted Function Example | Python sorted() Method

0

Python sorted() is an inbuilt Function that sorts any sequence for example list, tuple, and a dictionary which always returns the value with the items in a sorted manner, without modifying an original series. The sorted() method sorts the items of the given iterable object in the specific order which is either Ascending or Descending.

Python Sorted Built-In Function Tutorial

The sorted() method returns a sorted list from the given iterable.

See the following syntax.

sorted(iterable object, key, reverse)
  1. Iterable:  An iterable sequence like list, tuple, string or collection like a dictionary, set, frozenset or any other iterator object that needs to be sorted.
  2. Key(optional):  The function that would serve as a key or a basis of sort comparison.
  3. Reverse(optional):  If it set to true, then an iterable object would be sorted in reverse or descending order. By default, it is set as False.

Python sorted method returns a sorted list from the given iterable.

Let’s see the following example.

Sorting a list using Python Sorted Method

Write the following code inside the app.py file.

// app.py

listA = ['k', 'k', 'a', 's', 'v']
print(sorted(listA))

See the output below.

Sorting a list using Python Sorted Method

See, we can see the sorted elements inside the list.

Now, let’s take a list with the integer items in it.

# app.py

listB = [21, 19, 46, 18, 29]
print(sorted(listB))

See the output.

Python Sorted Built-In Function Tutorial

Sorting a String using sorted() method

Let’s sort a string. See the following code.

# app.py

strA = 'AppDividend'
print(sorted(strA))

See the output.

Sorting a String using sorted() method

Now, let’s take a list of strings and sort that list.

# app.py

listStr = ['Venom', 'Toxin', 'Carnage', 'Knull']
print(sorted(listStr))

See the output.

Python Sorted Built-In Function

It will return the sorted strings in a list.

Add a key as a parameter in a Sorted Function

Let’s add a key as a parameter to the sorted() function.

# app.py

statementA = 'We do whatever we want because We are Venom'
print(sorted(statementA.split(), key=str.upper))

See the below.

Add a key as a parameter in a Sorted Function

Let’s add a reverse parameter to the Python Sorted function.

# app.py

strB = 'Amazon'
print(sorted(strB, reverse=True))

See the output.

Python Sorted Reverse Function Example

Sorting a Dictionary using Sorted Function

Let’s sort a dictionary.

# app.py

dictA = {
    19: 'Beautiful',
    21: 'Handsome',
    18: 'Gorgeous',
    46: 'Glamorous' 
}
print(sorted(dictA, reverse=True))

It will give us the output based on the dictionary keys.

See the output.

Sorting a Dictionary using Sorted Function

Sorting a Tuple using Python sorted() method

Let’s take the example of a Tuple.

# app.py

tupA = ('Penny', 'Sheldon', 'Raj', 'Leonard')
print(sorted(tupA))

It will output based on the Alphabetical order.

See the output.

Sorting a Tuple using Python sorted() method

Finally, Python Sorted Built-In Function Tutorial With Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.