II. Managing Lists
1. Common Methods for Sorting Lists
In Python, sorting a list can be categorized into two types: temporary sorting and permanent sorting. For temporary sorting, we can use the built-in Python function sorted(), while for permanent sorting, we can use the list method sort(). The default sorting rule for both the sorted() function and the sort() method is as follows: numbers are sorted in ascending order (i.e., in the order of 1, 2, 3, 4, 5, 6, …), and strings are sorted based on their Unicode code points (which can be likened to the “identity card number” of characters) in ascending order. During sorting, the first character’s code point is compared, and if they are the same, the next character is compared, such as 'apple' and 'apply'. List sorting is also case-sensitive; uppercase letters (A-Z) have lower code points than lowercase letters (a-z), so 'Apple' will always be sorted before 'apple'.
Example 1.1:
rich_man=['musk','buffett','altman'] # Create a list
rich_man.sort() # Sort the list using the sort() method
print(rich_man) # Output the list as ['altman','buffett','musk']
In Example 1.1, we used the sort() method to permanently sort the list rich_man. The default sorting rule is ascending order, so Example 1.1 sorts the list in alphabetical ascending order.
We can also use the sort() method to sort the list elements in reverse alphabetical order by simply passing the parameter reverse=True to the sort() method. Example 1.2:
rich_man=['musk','altman','buffett'] # Create a list
rich_man.sort(reverse=True) # Pass the reverse parameter to the sort() method and sort rich_man
print(rich_man) # Output the list as ['musk','buffett','altman']
All the above lists are permanently sorted using the sort() method. If we only want to temporarily sort the list, we can use the sorted() function. Example 2.1:
rich_man=['musk','buffett','altman'] # Create a list
print(sorted(rich_man)) # Temporarily sort the list using the sorted() function and output the list as ['altman','buffett','musk']
print(rich_man) # Output the original list as ['musk','buffett','altman'] to verify that the sorted() function performs a temporary ascending sort
As shown in Example 2.1, when we call the sorted() function on the list, it only temporarily modifies the order of the list elements, and the order remains in the default ascending order; the original list’s order is unchanged. For the sorted() function, we can also pass the parameter reverse=True to perform a reverse sort. Example 2.2:
rich_man=['buffett','musk','altman'] # Create a list
print(sorted(rich_man,reverse=True)) # Pass the required parameter to the sorted function and output the result as ['musk','buffett','altman']
print(rich_man) # Output the original list as ['buffett','musk','altman'] to verify that the sorted() function performs a temporary descending sort
Summary:
| Operation Type | Permanent Modification of List | Generate New List | Applicable Scenario |
| list.sort() | Yes | No | No need to retain the original list |
| sorted(list) | No | Yes | Need to retain original list data |
2. Reversing a List
In Python, in addition to sorting a list according to certain rules, we can also use the reverse() method to reverse the list. It is important to note that reverse() differs from the sorting rules mentioned above; it simply reverses the order of the original list. After reversal, the order of the elements in the list is completely opposite to that of the original list (i.e., in reverse order), rather than sorted based on value. Example 3:
rich_man=['musk','buffett','altman'] # Create a list
print(rich_man) # Output the original list as ['musk','buffett','altman']
rich_man.reverse() # Call the reverse() method
print(rich_man) # Output the new list as ['altman','buffett','musk']
The reverse() method will also permanently modify the list, but if we want to restore the list to its original state, we can simply call the reverse() method again.
3. Length of a List
In Python, the length of a list refers to the number of elements contained in the list. This is a very important basic property of a list, used to determine the size of the list, control loops, manage memory, and perform various logical operations. To obtain the length of a list, we can use the built-in Python function len(), which is also the most commonly used standard method for getting the length of a list. Example 4:
rich_man=['musk','buffett','altman'] # Create a list
print(len(rich_man)) # Call the len() function, outputting 3