• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Sort a List of Strings Using Python

Sort a List of Strings Using Python

December 3, 2021 Leave a Comment

To sort a list of strings using Python, the simplest way is to use the Python sort() function.

list_object.sort()

You can also sort a list of string using the Python sorted() function.

sorted_list_object = sorted(list_object)

Let’s say I have the following list of strings that I want to sort in Python:

l = ["this","is","a","list","of","strings"]

We can sort the list of strings with the sort() function. The sort() function manipulates our list object directly.

l = ["this","is","a","list","of","strings"]

l.sort()

print(l) 

#output:
["a","is","list","of","strings","this"]

We can also sort a list of strings using the sorted() function. The sorted() function takes a list of objects as a parameter and returns a new list which is sorted.

l = ["this","is","a","list","of","strings"]

new_l = sorted(l)

print(new_l) 

#output:
["a","is","list","of","strings","this"]

Sorting a List of Strings in Descending Order in Python

Sometimes, when we have a list of strings, we want to sort the strings in descending order. We can sort the list in descending order by passing a value for the “reverse” parameter to the sort() function.

Let’s say I have the following list of strings:

l = ["this","is","a","list","of","strings"]

To sort the list of strings in descending order, we can pass “reverse=true” to the sort() function. The following code will sort a list of strings in reverse:

l = ["this","is","a","list","of","strings"]

l.sort(reverse=true)

print(l) 

#output:
["this","strings","of","list","is","a"]

We can sort the list of strings in descending order using the sorted() function by passing “reverse=true”.

The code below shows how you would use the sorted() function to sort a list of strings in reverse.

l = ["this","is","a","list","of","strings"]

new_l = sorted(l,reverse=true)

print(new_l) 

#output:
["this","strings","of","list","is","a"]

Sorting a List of Integer Strings in Python

If we have a list of strings which are integers, we can sort the list by passing a value for the “key” parameter to the sort() function.

Let’s say I have the following list of strings. The strings in this list are all integer numbers.

l = ["100","12","34","1","5","56","78"]

To sort the list of strings made up of integers, we can pass “key=int” to the sort() function. The following code will sort a list of strings made up of integers:

l = ["100","12","34","1","5","56","78"]

l.sort(key=int)

print(l) 

#output:
["1","5","12","34","56","78","100"]

We can sort the list of strings made up of integers using the sorted() function by passing “key=int”.

The code below shows how you would use the sorted() function to sort a list of strings which are integers.

l = ["100","12","34","1","5","56","78"]

new_l = sorted(l,key=int)

print(new_l) 

#output:
["1","5","12","34","56","78","100"]

You can also combine “reverse=true” with “key=int” to sort the list of strings made up of integers in descending order.

The code below shows you how to sort a list of strings which are integers in reverse:

l = ["100","12","34","1","5","56","78"]

new_l = sorted(l,key=int, reverse=true)

print(new_l) 

#output:
["100","78","56","34","12","5","1"]

Sorting a List of Strings with Another Function in Python

Something really cool about the sort() and sorted() functions is that the “key” parameter can take any function as input.

In the example above with “key=int”, the sorted function is applying the Python int() function to each of the string elements and then sorting.

We can define our own function to sort a list of strings. Let’s say I have the following list of strings:

l = ["this","is","another","list","of","strings"]

Let’s say I want to sort the list of strings by the second letter of each word. We define a function which returns the second letter of a string, and we pass that to the “key” parameter in the sorted() function as shown below:

l = ["this","is","another","list","of","strings"]

def second_letter(s):
    return s[1]

new_l = sorted(l,key=second_letter)

print(new_l) 

#output:
['of', 'this', 'list', 'another', 'is', 'strings']

Hopefully this article has helped you understand how to sort a list of strings in Python.

Other Articles You'll Also Like:

  • 1.  Reverse a List in Python Without Reverse Function
  • 2.  Get Random Value from Dictionary in Python
  • 3.  Remove Brackets from String Using Python
  • 4.  Using Python to Initialize Array of Size N
  • 5.  How to Ask a Question in Python
  • 6.  Change Python Turtle Background Color with screensize() Function
  • 7.  Remove Empty Strings from List in Python
  • 8.  Python Increment Counter with += Increment Operator
  • 9.  Using Python to Count Odd Numbers in List
  • 10.  How to Group By Columns and Find Variance in pandas

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy