DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk2 min

How to Sort a List Alphabetically in Python

Use sorted() for a new alphabetized list and list.sort() to reorder one in place. Add key=str.casefold for case-insensitive string sorting.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use sorted(words) to create a new alphabetically ordered list, or words.sort() to reorder a list in place. For case-insensitive sorting, pass key=str.casefold to either method.

Choose between a new list and sorting in place

Python provides two built-in options. sorted() accepts any iterable and returns a new list, leaving the original sequence unchanged. list.sort() changes the existing list and returns None. The Python Sorting HOW TO explains both behaviors.

What you need Use What happens
Keep the original unchanged, or sort an iterable such as a tuple sorted(items) Returns a new list
Reorder an existing list items.sort() Mutates the list; returns None

Make a sorted copy

Call sorted() when you want the result in a separate list:

words = ["pear", "apple", "banana"]
alphabetical = sorted(words)

print(alphabetical)  # ['apple', 'banana', 'pear']
print(words)         # ['pear', 'apple', 'banana']

Reorder the existing list

Call .sort() when changing the list itself is intended:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
words = ["pear", "apple", "banana"]
words.sort()

print(words)  # ['apple', 'banana', 'pear']

Do not assign the method’s return value back to the list: words = words.sort() makes words equal to None.

Sort strings without distinguishing uppercase and lowercase

Default string sorting distinguishes uppercase and lowercase. To compare strings without that distinction, use the built-in str.casefold method as the key:

names = ["Zoe", "alice", "Émile"]
case_insensitive = sorted(names, key=str.casefold)

print(case_insensitive)  # ['alice', 'Zoe', 'Émile']

The key determines what Python compares; it does not replace the original strings. A key is calculated once per item during sorting. You can use the same option with .sort():

names.sort(key=str.casefold)

Sort in descending order

Pass reverse=True to put the sorted result in descending order. It works with both sorting methods and can be combined with a key:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
reverse_order = sorted(names, key=str.casefold, reverse=True)

Python’s sort is stable: when items have equal sort keys, their relative order from the input is preserved. This is useful, for example, when a key intentionally treats differently cased strings as equivalent.

Sort records by a name field

For objects with a name attribute, use a key that selects that attribute. For tuples with the name in the first position, use an item accessor:

people_by_name = sorted(people, key=lambda person: person.name)
rows_by_name = sorted(rows, key=operator.itemgetter(0))

These examples assume the selected values can be compared with one another. A key changes the comparison basis while leaving the stored objects or records intact.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When language-specific alphabetization matters

Case-insensitive sorting is not the same as dictionary ordering in every language. If the ordering must follow a particular locale’s rules, Python’s sorting guide describes using locale.strxfrm() as a key or locale.strcoll() as a comparison function. These functions follow the active locale, so the application needs to set the intended locale for its environment. See the Python Sorting HOW TO for details.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Handle values that cannot be compared directly

The examples above assume strings, or selected key values, are mutually orderable. If a list contains mixed or otherwise unorderable values, provide an explicit key that maps every item to a comparable value; without one, Python may raise a TypeError.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.