**How to sort a list, string, tuple in Python?**

Python provides two main ways to sort data structures like lists, strings, and tuples:

and **sort()**

.**sorted()**

Let’s dive into each of these methods and see how they work.

**2 Ways to Sort: sort(), sorted()**

`sort()` | `sorted()` | |
---|---|---|

Works on | Only lists | Both sequences and collections |

Return value | None (sorts the list in-place) | A new sorted list |

Effect on original sequence | Modifies the original list | Creates a sorted copy |

Parameters | `key` , `reverse` | `iterable` , `key` , `reverse` |

Syntax | `list_name.sort(key, reverse=False)` | `sorted(iterable, key, reverse = True)` |

There are 2 Python methods to sort objects. One is sort(), and the other is sorted().

**In this article, I’ll explain how to use each of them, and after that I’ll show you the differences of them.**

**Method 1: Using **`sort()`

`sort()`

The `sort()`

method is a built-in function of the list data type.

**It sorts the original list in place**, which means it modifies the list it is called on.

**Basic Syntax**

The basic syntax of the `sort()`

method is as follows:

`list.sort(reverse=False)`

The `reverse`

parameter is optional and defaults to `False`

, which means the list will be sorted in ascending order.

If `reverse=True`

, the list will be sorted in descending order.

**Simple Code Example**

Here’s an example of how to use the `sort()`

method:

```
numbers = [3, 1, 4, 5, 2]
numbers.sort()
print(numbers) # Output: [1, 2, 3, 4, 5]
```

In this example, the list of numbers is sorted in ascending order.

**Method 2: Using **`sorted()`

`sorted()`

The `sorted()`

function is a built-in Python function that returns a new list containing all items from the original list in ascending order.

**It does not modify the original list.**

**Basic Syntax**

The basic syntax of the `sorted()`

function is as follows:

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

The `iterable`

parameter is required and is the sequence (string, tuple, etc.) or collection (set, dictionary, etc.) or an iterator object to be sorted.

The `reverse`

and `key`

parameters are optional. `reverse=False`

means the iterable will be sorted in ascending order, and `reverse=True`

means it will be sorted in descending order.

**Simple Code Example**

Here’s an example of how to use the `sorted()`

function:

```
numbers = [3, 1, 4, 5, 2]
sorted_numbers = sorted(numbers)
print(sorted_numbers) # Output: [1, 2, 3, 4, 5]
print(numbers) # Output: [3, 1, 4, 5, 2]
```

In this example, a new list is created with the sorted numbers, while the original list remains unchanged.

**Sorting Strings and Tuples**

Since strings and tuples are immutable, you can’t use `sort()`

to modify them in place.

**However, sorted() accepts any iterable objects, including lists, strings, and tuples, and returns a new sorted list.**

**Sorting Strings**

When a string is passed to `sorted()`

, it returns a list of sorted characters.

```
s = 'cebad'
sorted_s = ''.join(sorted(s))
print(sorted_s) # Output: 'abcde'
```

In this example, the string ‘cebad’ is sorted in ascending order to ‘abcde’.

**Sorting Tuples**

Sorting tuples is similar to sorting strings. `sorted()`

returns a sorted list when a tuple is passed.

```
t = (3, 1, 4, 5, 2)
sorted_t = tuple(sorted(t))
print(sorted_t) # Output: (1, 2, 3, 4, 5)
```

In this example, the tuple `(3, 1, 4, 5, 2)`

is sorted in ascending order to `(1, 2, 3, 4, 5)`

.

**Difference: sort() vs sorted()**

Let me explain the differences between `sort()`

and `sorted()`

in Python!

**Acceptable Objects**

`sorted()`

is a built-in Python function that accepts **any iterable**, while `sort()`

is a method **specific to lists.**

**Return Value**

`sorted()`

returns a new sorted list, leaving the original iterable unchanged.

On the other hand, `sort()`

**does not return a new list**; instead, it sorts the original list in place, meaning the original list is changed.

**Memory Usage**

`sorted()`

creates a new list containing a sorted version of the iterable, which can lead to more memory use if you’re sorting large iterables.

In contrast, `sort()`

sorts the list in-place and does not use extra memory.

**Code showing the Difference**

`sort()` | `sorted()` | |
---|---|---|

Works on | Only lists | Both sequences and collections |

Return value | None (sorts the list in-place) | A new sorted list |

Effect on original sequence | Modifies the original list | Creates a sorted copy |

Parameters | `key` , `reverse` | `iterable` , `key` , `reverse` |

Syntax | `list_name.sort(key, reverse=False)` | `sorted(iterable, key, reverse = True)` |

```
# Using sort()
numbers = [3, 1, 4, 1, 5, 9, 2]
numbers.sort()
print(numbers) # Output: [1, 1, 2, 3, 4, 5, 9]
# Using sorted()
numbers = [3, 1, 4, 1, 5, 9, 2]
sorted_numbers = sorted(numbers)
print(numbers) # Output: [3, 1, 4, 1, 5, 9, 2]
print(sorted_numbers) # Output: [1, 1, 2, 3, 4, 5, 9]
```

In the first example, `sort()`

is used, and you can see that the original list `numbers`

is sorted.

In the second example, `sorted()`

is used, and you can see that the original list `numbers`

remains unchanged, and a new sorted list `sorted_numbers`

is created.