Skip to main content

Command Palette

Search for a command to run...

sorted(List) vs List.sorted() in Python

Updated
1 min read
sorted(List) vs List.sorted() in Python
C

Senior Data Engineer • Contractor / Freelancer • GCP & AWS Certified

When I was just starting out with Python I found the distinction between a function doing something in place and returning the result instead pretty interesting.

Understanding this distinction is pretty important as a beginner! Since sorting in one of the most important operations we do in programming, let's look at an example involving exactly that.

➡ sorted([a_list]): is a built-in function than returns a new sorted list, without modifying the original
➡ a_list.sort(): sorts the list in place and doesn't return anything

Found it useful? Subscribe to my Analytics newsletter at notjustsql.com.

More from this blog

D

Datawise: A blog on SQL, BigQuery, Analytics, Python Tips and Tricks

204 posts

Data Engineer with a passion for transforming complex data landscapes into insightful stories. Here on my blog, I share insights, challenges, and the ever-evolving dance of technology and business.