# Python Sorted() Function Example

In Python, the `sorted()`

function is a versatile tool for sorting iterable objects.

It provides a convenient way to arrange elements in ascending or descending order, facilitating a wide range of applications from simple lists to more complex data structures.

In this article, we'll delve into the `sorted()`

function, exploring its syntax, customization options, and practical examples.

## Basic Syntax:

The basic syntax of the `sorted()`

function is as follows:

```
sorted(iterable, key=None, reverse=False)
```

`iterable`

: The iterable (e.g., a list, tuple, or string) to be sorted.`key`

: A function that generates a key for each element, used for custom sorting.`reverse`

: If`True`

, the iterable is sorted in descending order; if`False`

(default), it is sorted in ascending order.

## Simple Examples:

### Sorting a List:

```
numbers = [5, 2, 8, 1, 3]
sorted_numbers = sorted(numbers)
print("Original List:", numbers)
print("Sorted List:", sorted_numbers)
```

In this example, the `sorted()`

function is applied to a list of numbers, resulting in a new list with elements sorted in ascending order.

### Sorting a String:

```
text = "python"
sorted_text = sorted(text)
print("Original String:", text)
print("Sorted String:", ''.join(sorted_text))
```

Here, the characters of the string "python" are sorted alphabetically using `sorted()`

, and the result is joined into a new string.

## Custom Sorting with the `key`

Parameter:

The `key`

parameter allows you to specify a custom function to determine the sorting order.

For instance, you can sort a list of strings based on their lengths.

```
words = ["apple", "banana", "kiwi", "orange"]
sorted_words = sorted(words, key=len)
print("Original List:", words)
print("Sorted List by Length:", sorted_words)
```

In this example, the `len`

function is used as the key to sort the list of words based on their lengths.

## Sorting in Descending Order:

To sort in descending order, set the `reverse`

parameter to `True`

.

```
numbers = [5, 2, 8, 1, 3]
sorted_numbers_desc = sorted(numbers, reverse=True)
print("Original List:", numbers)
print("Sorted List (Descending):", sorted_numbers_desc)
```

Here, the `sorted()`

function is used to sort a list of numbers in descending order.

## Sorting Tuples and Sets:

The `sorted()`

function is not limited to lists; it can also be applied to tuples and sets.

```
my_tuple = (10, 5, 8, 2, 7)
sorted_tuple = sorted(my_tuple)
my_set = {3, 1, 4, 1, 5, 9, 2}
sorted_set = sorted(my_set)
print("Original Tuple:", my_tuple)
print("Sorted Tuple:", sorted_tuple)
print("Original Set:", my_set)
print("Sorted Set:", sorted_set)
```

In this example, both a tuple and a set are sorted using the `sorted()`

function.

## Practical Example with Dictionaries:

The `sorted()`

function can be applied to dictionaries, considering either keys or values for sorting.

```
my_dict = {"apple": 5, "banana": 2, "kiwi": 8, "orange": 1, "melon": 3}
sorted_dict_by_key = dict(sorted(my_dict.items()))
sorted_dict_by_value = dict(sorted(my_dict.items(), key=lambda x: x[1]))
print("Original Dictionary:", my_dict)
print("Sorted Dictionary by Key:", sorted_dict_by_key)
print("Sorted Dictionary by Value:", sorted_dict_by_value)
```

Here, the dictionary is sorted by keys in the first case and by values in the second case using the `key`

parameter.

## Conclusion:

The `sorted()`

function in Python is a powerful tool for sorting iterable objects, providing flexibility and customization options.

Whether you're working with simple lists, strings, tuples, sets, or dictionaries, the `sorted()`

function allows you to arrange elements in the desired order.

Understanding its syntax and parameters empowers you to tailor the sorting process to fit your specific needs.

Experiment with different use cases and embrace the versatility of the `sorted()`

function in your Python projects. Happy coding!