• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Python / Lambda Function and Sort / How to Sort Lists in-place in Python?

How to Sort Lists in-place in Python?

June 6, 2012 by cmdlinetips

Let us starting simple and start with easy sort. Python has two sort functions; one function that sorts a list in-place and the other sort function “sorted” that creates a new sorted list. In this post, we will see examples of “in-place” sorting.

Python’s in-place sorting

As the name suggests the in-place sorting replaces the original un-sorted list to a new sorted list. If you have a list named myList and want to sort it using in-place sorting,

just call the built-in method as myList.sort()

For example,

>myList = [66,11,88, 44, 77]
>myList.sort()
>myList
[11, 44, 66, 77, 88]

Remember, myList can contain a numbers or strings. For example, if you are sorting strings, Python will sort it alphabatically as

>myList=['line', 'cmd', 'tips']
>myList.sort()
>myList
['cmd', 'line', 'tips']

How to Sort a List Numerically?

Often you might have numbers as string and you might want to sort numerically. However, Python will do it alphabatically. For example, if you sort the list

>myList = ['50', '111', '1', '99', '10']

Python will do the sort alphabetically and give you

>myList.sort()
>myList
['1', '10', '111', '50', '99']

If you really want to sort the string list by treating it as numeric, you can use the option “key” in Python sort function as

>myList = ['50', '111', '1', '99', '10']
>myList.sort(key=int)
>myList
['1', '10', '50', '99', '111']

Often you will want to sort a list of strings based on some specific criteria in the string (a specific part of the string). One way to do is to use lambda function. For example a list with with numeric values as part of the string.

>myList =['cmd1','cmd10', 'cmd111', 'cmd50', 'cmd99']

Using myList.sort() will sort the list alphabatically.

>myList.sort()
>myList
['cmd1', 'cmd10', 'cmd111', 'cmd50', 'cmd99']

How to Sort a List in Python using Lambda Functions?

Often, one may want to sort a list but based on part of the elements of the items in the list. For example, you want to sort a list of strings using the numerical part of the string.

We can use write custom functions or lambda functions to sort the list according the numeric value in each of string in the list. For example

>myList =['cmd1','cmd10', 'cmd111', 'cmd50', 'cmd99']
# sort numerically with lambda function
>myList.sort(key=lambda x: int(x[3:]))
>myList
['cmd1', 'cmd10', 'cmd50', 'cmd99', 'cmd111']

where the key is a lambda function that takes string as input and the numeric value as output.

Here is one more example of sorting lists using lambda function. Again we will be sorting a list of string based on part of string.

>myList = ['cmd_line1','cmd_line10.com', 'cmd_line111', 'cmd_line50', 'cmd_line99']
# sort numerically with lambda function
>myList.sort(key=lambda x: int(x.split('_')[1][4:]))
>myList
['cmd_line1', 'cmd_line10.com', 'cmd_line111', 'cmd_line50', 'cmd_line99']

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default ThumbnailGetting Started With Map, Filter, and Reduce in Python Default ThumbnailHow to Use Lambda Functions in Python? Default ThumbnailMastering Lists in Python Using List Comprehensions Default ThumbnailThree Ways to Merge (or flatten) Lists in Python

Filed Under: Lambda Function and Sort, Python, Python In-Place Sorting, Python Sort, Python Tips Tagged With: Lambda Function and Sort, Python In-Place Sorting, Python Sort, sort a list numerically

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version