Python Sort List By Alphabetically
## Python: How to Sort a List Alphabetically
In Python, sorting a list of strings alphabetically is a common task. Python provides two highly efficient, built-in ways to achieve this:
1. **The `sort()` method**: Modifies the original list in-place. It does not create a new list and returns `None`.
2. **The `sorted()` function**: Returns a new sorted list, leaving the original list unmodified.
This tutorial covers how to use both approaches, how to sort in reverse (descending) order, and how to handle common edge cases like case sensitivity.
---
## 1. In-Place Sorting with the `sort()` Method
Use the `list.sort()` method when you want to sort the list directly and do not need to preserve the original order of the elements. Because it modifies the list in-place, it is highly memory-efficient.
### Syntax
```python
list.sort(key=None, reverse=False)
```
### Example: Ascending Alphabetical Order
```python
# Initialize a list of strings
fruits = ["cherry", "apple", "date", "banana"]
# Sort the list in-place alphabetically
fruits.sort()
# Output the modified list
print(fruits)
```
**Output:**
```python
['apple', 'banana', 'cherry', 'date']
```
---
## 2. Creating a New Sorted List with the `sorted()` Function
Use the built-in `sorted()` function when you want to keep the original list unchanged and create a new, sorted copy.
### Syntax
```python
sorted_iterable = sorted(iterable, key=None, reverse=False)
```
### Example: Ascending Alphabetical Order
```python
# Initialize a list of strings
fruits = ["cherry", "apple", "date", "banana"]
# Create a new sorted list
sorted_fruits = sorted(fruits)
# Output both lists to show the original remains unchanged
print("Original list:", fruits)
print("New sorted list:", sorted_fruits)
```
**Output:**
```python
Original list: ['cherry', 'apple', 'date', 'banana']
New sorted list: ['apple', 'banana', 'cherry', 'date']
```
---
## 3. Sorting in Reverse (Descending) Alphabetical Order
To sort a list in reverse alphabetical order (Z to A), pass the parameter `reverse=True` to either the `sort()` method or the `sorted()` function.
### Example using `sort()` (In-Place):
```python
fruits = ["cherry", "apple", "date", "banana"]
# Sort in-place in reverse alphabetical order
fruits.sort(reverse=True)
print(fruits)
```
**Output:**
```python
['date', 'cherry', 'banana', 'apple']
```
### Example using `sorted()` (New List):
```python
fruits = ["cherry", "apple", "date", "banana"]
# Create a new list sorted in reverse alphabetical order
reverse_sorted_fruits = sorted(fruits, reverse=True)
print(reverse_sorted_fruits)
```
**Output:**
```python
['date', 'cherry', 'banana', 'apple']
```
---
## 4. Advanced Considerations
### Case-Insensitive Sorting
By default, Python sorts strings based on ASCII/Unicode values, meaning uppercase letters (A-Z) are sorted before lowercase letters (a-z).
To perform a true, case-insensitive alphabetical sort, use the `key` parameter with `str.lower`.
```python
# Mixed-case list
mixed_fruits = ["cherry", "Apple", "date", "Banana"]
# Default sorting (Case-sensitive: Uppercase comes first)
default_sorted = sorted(mixed_fruits)
print("Default sorted:", default_sorted)
# Case-insensitive sorting
case_insensitive_sorted = sorted(mixed_fruits, key=str.lower)
print("Case-insensitive sorted:", case_insensitive_sorted)
```
**Output:**
```python
Default sorted: ['Apple', 'Banana', 'cherry', 'date']
Case-insensitive_sorted: ['Apple', 'Banana', 'cherry', 'date'] # (Sorted correctly regardless of case)
```
### Summary: `sort()` vs `sorted()`
| Feature | `list.sort()` | `sorted(list)` |
| :--- | :--- | :--- |
| **Modification** | Modifies the list in-place. | Returns a new sorted list. |
| **Original List** | Altered permanently. | Remains unchanged. |
| **Return Value** | Returns `None`. | Returns the sorted list. |
| **Data Types** | Works only on lists. | Works on any iterable (tuples, dictionaries, sets, etc.). |
YouTip