![]() |
|
[Tut] How to Sort and Return a Python List in One Line? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Sort and Return a Python List in One Line? (/thread-97131.html) |
[Tut] How to Sort and Return a Python List in One Line? - xSicKxBot - 09-09-2020 How to Sort and Return a Python List in One Line? To sort and return a Python list in a single line of code, use the Code: sorted(list)![]() Let’s dive into the challenge to learn about more details and alternatives. Everything is not always simple. By studying the different methods that solves this, you’ll become a better coder! Problem: Given a listof comparable objects such as integers or floats. Is there a way to sort the list and return the sorted list in a single line of Python code? Example: Say, you’ve got the following list. Code: a = [4, 2, 1, 3]You want to sort this list and return the result in a single line. If you use the Code: list.sort()Code: NoneCode: print(a.sort())
# NoneThe return value of the Code: list.sort()Code: NoneCode: NoneHowever, returning Code: NoneCode: list.sort()So, how to sort and return a list in a single line of Python code? As a rule of thumb, there are always multiple ways to accomplish the same thing in Python. Let’s dive into the different ways to accomplish this! Here’s a quick overview of the methods addressed in this article: https://repl.it/@finxter/ModestAllMp3?lite=true Exercise: Change the list to be sorted by adding negative floats. Does it still work? You’ll now learn more about each of the methods. Method 1: sorted() The easiest way to accomplish this task is to call Python’s built-in Code: sorted()Code: a = [4, 2, 1, 3] # Method 1: sorted()
print(sorted(a))The Code: sorted()Code: print()Code: [1, 2, 3, 4]This method is the most Pythonicone. But are there alternatives? Method 2: list.sort() + Ternary Operator The Code: sorted()The answer is simple: use a combination of the Code: list.sort()Code: a = [4, 2, 1, 3] # Method 2: list.sort() + ternary
print(a if a.sort() else a)
# [1, 2, 3, 4]You need to understand two concepts: (1) Code: list.sort()
The beautiful thing is that the one-liner Code: print(a if a.sort() else a)Explanation: First, the Code: a.sort()Code: a.sort()Code: NoneCode: NoneCode: FalseCode: aNote that the only purpose of the ternary operator is to make sure to call the Code: a.sort()Code: aIf you print the original list to the shell, you’ll see that it is now sorted: Code: >>> print(a)
[1, 2, 3, 4]An alternative is chaining the statements with a semicolon Code: ;Code: a = [4, 2, 1, 3] # Method 3: semicolon
a.sort(); print(a)
# [1, 2, 3, 4]If you need to sort a Python list and print its return value to the shell—for example because you run this command from the command line or terminal—you can use this excellent strategy. You can learn more about how to one-linerize any Python program in my following video: Python One-Liners Book Python programmers will improve their computer science skills with these useful one-liners. ![]() Python One-Linerswill teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert. The book’s five chapters cover tips and tricks, regular expressions, machine learning, core data science topics, and useful algorithms. Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments. You’ll also learn how to: • Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution • Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics • Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning • Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators • Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line. Get your Python One-Liners Now!! |