To type and also rerevolve a Python list in a single line of code, use the sorted(list) strategy that retransforms a new list of sorted aspects. It copies just the references to the original elements so the changed list is not a deep but a shpermit copy.

You watching: Sort returns none python

Let’s dive into the challenge to learn about even more details and options. Everything is not always straightforward. By examining the various approaches that solves this, you’ll end up being a much better coder!

**Problem**: Given a list of equivalent objects such as integers or floats. Is there a method to kind the list and also return the sorted list in a single line of Python code?

**Example**: Say, you’ve obtained the following list.

a = <4, 2, 1, 3>You desire to kind this list and rerotate the lead to a solitary line. If you usage the list.sort() technique, the rerevolve value is None:

print(a.sort())# NoneThe rerevolve worth of the list.sort() approach is None, yet many type of coders intend it to be the sorted list. So they’re surprised finding out that their variables contain the None kind rather than a sorted list.

However, returning None provides perfect sense for the list.sort() method. Why? Because you contact the strategy on a list object and it modifies this precise list object. It doesn’t create a brand-new list—tbelow won’t be a new list object in memory.

So, how to type and rerotate a list in a single line of Python code? As a ascendancy of thumb, there are always multiple ways to achieve the exact same thing in Python. Let’s dive right into the various methods to accomplish this!

Here’s a quick overcheck out of the techniques addressed in this article:

**Exercise**: Change the list to be sorted by including negative floats. Does it still work?

You’ll currently learn even more around each of the approaches.

Table of Contents

## Method 1: sorted()

The easiest method to accomplish this task is to call Python’s built-in sorted() function that takes an iterable and retransforms a brand-new list via sorted elements.

See more: Free Monty Python Holy Grail Ringtones For Your Mobile Phone

a = <4, 2, 1, 3># Method 1: sorted()print(sorted(a))The sorted() feature generates a new sorted list that is put right into the print() function that prints the sorted list to the shell. The output is the sorted list:

<1, 2, 3, 4>This method is the the majority of Pythonic one. But are tbelow alternatives?

## Method 2: list.sort() + Ternary Operator

The sorted() technique leaves the original list unreadjusted. But what if you desire to type the original list and gain this original list as an output that you have the right to asauthorize to a variable?

The answer is simple: use a combination of the list.sort() strategy and also the ternary operator!

a = <4, 2, 1, 3># Method 2: list.sort() + ternaryprint(a if a.sort() else a)# <1, 2, 3, 4>You have to understand 2 concepts: (1) list.sort() and (2) the ternary operator:

The beautiful point is that the one-liner print(a if a.sort() else a) modifies the original list and also returns it right away. How does it perform this?

**Explanation**: First, the a.sort() technique is referred to as to inspect which “branch” of the ternary operator must be visited. The return worth of a.sort() will certainly constantly be None. The None worth is immediately converted to the Boolean False. Thus, the ternary operator always retransforms the list object described by variable a.

Keep in mind that the only objective of the ternary operator is to make certain to contact the a.sort() method prior to returning the worth a—to make certain it is sorted!

If you print the original list to the shell, you’ll see that it is now sorted:

## Method 3: Combining Multiple Statements in a Single Line through Semicolon

An different is chaining the statements via a semicolon ; to one-linerize a Python code snippet. This strategy functions through level Python programs without, possible nested, blocks:a = <4, 2, 1, 3># Method 3: semicolona.sort(); print(a)# <1, 2, 3, 4>If you should kind a Python list and print its rerotate value to the shell—for example because you run this command also from the command line or terminal—you can use this great strategy.

You have the right to learn more about exactly how to one-linerize any kind of Python regime in my adhering to video:

*Python One-Liners* will teach you exactly how to read and also create “one-liners”: ** concise statements of advantageous usability packed into a single line of code. **You’ll learn how to systematically unfill and understand also any type of line of Python code, and also write eloquent, powerfully compressed Python like an experienced.

The book’s five chapters cover (1) tips and also tricks, (2) continual expressions, (3) machine discovering, (4) core information science topics, and also (5) beneficial algorithms.

Detailed explanations of one-liners present ** key computer scientific research ideas **and

**. You’ll learn around progressed Python functions such as**

*rise your coding and also analytical skills**,*

**list comprehension****,**

*slicing***,**

*lambda functions***,**

*consistent expressions***and**

*map***functions, and**

*mitigate***.**

*slice assignments*You’ll also learn exactly how to:

Leverage data frameworks to**resolve real-civilization problems**, choose utilizing Boolean indexing to uncover cities with above-average pollutionUse

**NumPy basics**such as

*array*,

*shape*,

*axis*,

*type*,

*broadcasting*,

*advanced indexing*,

*slicing*,

*sorting*,

*searching*,

*aggregating*, and

*statistics*Calculate standard

**statistics**of multidimensional information arrays and also the K-Means algorithms for unsupervised learningCreate more

**advanced continuous expressions**utilizing

*grouping*and also

*called groups*,

*negative lookaheads*,

*escaped characters*,

*whitespaces, character sets*(and

*negative personalities sets*), and also

*greedy/nongreedy operators*Understand also a vast selection of

**computer scientific research topics**, including

*anagrams*,

*palindromes*,

*supersets*,

*permutations*,

*factorials*,

*prime numbers*,

*Fibonacci*numbers,

*obfuscation*,

*searching*, and also

*algorithmic sorting*

By the finish of the book, you’ll recognize exactly how to ** create Python at its a lot of refined**, and also develop concise, beautiful pieces of “Python art” in simply a single line.