Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. list.sort() / list.reverse() / list.copy()

list.sort() / list.reverse() / list.copy()

Since: sort() / reverse() Python 2(2000)
copy() Python 3.3(2012)

These methods sort a list, reverse its order, or create a copy of it. A key characteristic is that they modify the original list directly.

Syntax

# Sorts the list in place (returns None).
list.sort(key=None, reverse=False)

# Reverses the order of the list's elements in place (returns None).
list.reverse()

# Returns a shallow copy of the list.
list.copy()

# sorted() returns a new sorted list without modifying the original.
sorted(iterable, key=None, reverse=False)

Method and Function List

Method / FunctionDescription
list.sort(key, reverse)Sorts the list in place. The original list is modified. Returns None.
list.reverse()Reverses the order of the list's elements in place. The original list is modified. Returns None.
list.copy()Returns a shallow copy of the list. Equivalent to list[:].
sorted(iterable, key, reverse)Returns a new sorted list without modifying the original iterable. Works with any iterable, not just lists.

Sample Code

sample_list_sort_reverse.py
# Sort a list in place using sort().
nums = [3, 1, 4, 1, 5, 9, 2, 6]
nums.sort()
print(nums)  # [1, 1, 2, 3, 4, 5, 6, 9]

# Sort in descending order using reverse=True.
nums.sort(reverse=True)
print(nums)  # [9, 6, 5, 4, 3, 2, 1, 1]

# Sort with a custom key function.
pilots = ["Soryu Asuka", "Ayanami Rei", "Ikari Shinji", "Nagisa Kaworu"]
pilots.sort(key=len)  # Sort by string length.
print(pilots)  # Sorted from shortest to longest string.

# Another example using a key function with sort().
names = ["Vegeta", "son goku", "BULMA", "krillin"]
names.sort(key=str.lower)  # Sort case-insensitively.
print(names)  # ['BULMA', 'krillin', 'son goku', 'Vegeta']

# Sort a list of dictionaries by a key.
students = [
    {"name": "Ayanami Rei", "score": 92},
    {"name": "Ikari Shinji", "score": 85},
    {"name": "Soryu Asuka", "score": 78},
]
students.sort(key=lambda s: s["score"])
print(students[0]["name"])  # Soryu Asuka (the lowest score)

# Reverse the order of a list using reverse().
items = [1, 2, 3, 4, 5]
items.reverse()
print(items)  # [5, 4, 3, 2, 1]

# Create a copy of a list using copy().
original = [1, 2, 3]
copied = original.copy()
copied.append(4)
print(original)  # [1, 2, 3] (the original list is unchanged)
print(copied)    # [1, 2, 3, 4]

# sorted() does not modify the original list.
nums2 = [3, 1, 4, 1, 5]
sorted_nums = sorted(nums2)
print(nums2)        # [3, 1, 4, 1, 5] (unchanged)
print(sorted_nums)  # [1, 1, 3, 4, 5] (new list)

# Sort a list of strings using sorted().
result = sorted(["Son Goku", "Bulma", "Vegeta"])
print(result)  # ['Bulma', 'Son Goku', 'Vegeta']
python3 list_sort_reverse.py
[1, 1, 2, 3, 4, 5, 6, 9]
[9, 6, 5, 4, 3, 2, 1, 1]
['Soryu Asuka', 'Ayanami Rei', 'Ikari Shinji', 'Nagisa Kaworu']
['BULMA', 'krillin', 'son goku', 'Vegeta']
Soryu Asuka
[5, 4, 3, 2, 1]
[1, 2, 3]
[1, 2, 3, 4]
[3, 1, 4, 1, 5]
[1, 1, 3, 4, 5]
['Bulma', 'Son Goku', 'Vegeta']

Notes

The key difference between sort() and sorted() is whether the original list is modified. sort() modifies the original list in place and returns None. If you try to assign the result to a new variable, it will be None. Use sorted() whenever you need to preserve the original list.

Python's sorting algorithm is Timsort, which is a stable sort — meaning elements with equal values maintain their relative order. This makes it easy to sort by multiple keys in stages (for example, first by name, then by score) and have it work as expected.

copy() creates a shallow copy of the list. If the list contains objects such as other lists or dictionaries, those objects are not copied — only references to them are shared. For a fully independent copy, use copy.deepcopy(). For slicing and concatenation of lists, see List Slicing.

If you find any errors or copyright issues, please .