AppDividend
Latest Code Tutorials

Python Sorted Built-In Function Tutorial With Example

0

Python Sorted Built-In Function Tutorial With Example is today’s topic. Sorted() method 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. Sorting any sequence is very easy in Python using a built-in method sorted() which does all the hard work for you. 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. The syntax is following.

sorted(iterable object, key, reverse)
  • 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.
  • Key(optional):  The function that would serve as a key or a basis of sort comparison.
  • 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 below.

 

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))

The output is following.

 

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))

 

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 output.

 

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 below 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. The output is following.

 

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.

 

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.