Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

Why does `list.sort()` return `None` instead of the sorted list?

Quick answer

list.sort() sorts the existing list in place and returns None; it is not designed to produce a sorted list as its result. So items = items.sort() replaces the name items with None. Call items.sort() on its own when you want to change that list, or use sorted(items) when you want a new sorted list and need to keep the original unchanged. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I wrote items = items.sort() and now items is None. Why? I expected the sorted list to be assigned back to the variable.

Geez

sort() sorts the existing list in place, but its return value is None. In items = items.sort(), Python assigns that return value to items, so the name no longer refers to the list. [S1]

For example:

items = ["pear", "apple", "plum"]
items = items.sort()
print(items)  # None

The list was sorted by the method call, but the assignment replaced the useful reference with None. To sort that list, call the method without assigning its result:

items = ["pear", "apple", "plum"]
items.sort()
print(items)  # ['apple', 'pear', 'plum']

List-modifying methods such as sort() return None; the change to the list is the point of the call. [S1]

Follow-up question

What if I need the sorted version but also want to keep the original order available?

Geez

Use sorted(items) and store its result in a different name. It returns a new sorted list and leaves the source list unchanged. [S1]

items = ["pear", "apple", "plum"]
sorted_items = sorted(items)

print(sorted_items)  # ['apple', 'pear', 'plum']
print(items)         # ['pear', 'apple', 'plum']

Here, items remains in its original order, while sorted_items refers to the sorted result. The choice is whether you want to change the existing list or keep it as it is.

Follow-up question

So can I use sorted_items = items.sort() if I only need the sorted result? And what should I look for when debugging?

Geez

No. items.sort() returns None, so assigning its result to sorted_items would give that name the value None, not the sorted list. [S1] Use one of these patterns instead:

# Change the existing list
items.sort()

# Get a sorted list under a separate name
sorted_items = sorted(items)

When debugging, look for an assignment whose right-hand side ends in .sort(), especially one like items = items.sort(). If you meant to keep the sorted list in a variable, use sorted(...); if you meant to reorder the existing list, call .sort() as a standalone statement.

Follow-up question

Could you give me a short decision guide I can reuse?

Geez

Choose based on what should happen to the original list:

  1. To reorder the existing list, write items.sort() without assigning its result.
  2. To keep the original list unchanged and get a sorted list, write sorted_items = sorted(items).
  3. Avoid assigning the result of items.sort(): it returns None. [S1]

For example, these are the two alternatives:

# Sort the existing list
items = ["pear", "apple", "plum"]
items.sort()

# Make a sorted result and keep the original
items = ["pear", "apple", "plum"]
sorted_items = sorted(items)

If a variable unexpectedly becomes None, check whether it was assigned the result of .sort(). The key distinction is that sort() changes the existing list, while sorted() returns a new sorted list. [S1]

A practical checklist

  • Decide whether the existing list should be changed or kept in its original order.

  • For an in-place change, call items.sort() without assigning its result.

  • For a separate sorted list, assign sorted(items) to a new name.

  • Look for assignments such as items = items.sort() if a variable unexpectedly becomes None.

A few more useful answers

Does `items.sort()` leave the list unsorted because it returns `None`?

No. It sorts the existing list in place; None is the method's return value, not a sign that sorting failed. [S1]

How do I get a sorted list without changing the source list?

Use sorted(items) and store its result in another name. It returns a new sorted list and leaves the source unchanged. [S1]

Why does `items` become `None` after `items = items.sort()`?

The assignment stores the value returned by sort(). Since sort() returns None, the name items is rebound to None. [S1]

Sources

References used for this guide.

  1. [S1] Data Structures — List sorting and return values
    docs.python.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work