A Step-by-Step Guide to Organize Your Data Using Built-in Functions
Lists are an essential data structure in any programming language. In Python, you can sort a list by using built-in functions like sorted() or list.sort(). The method sorted() returns a new sorted lis …
Lists are an essential data structure in any programming language. In Python, you can sort a list by using built-in functions like sorted() or list.sort(). The method sorted() returns a new sorted list while the method list.sort() modifies the original list.
Table of Contents
Introduction
List is an important data type in Python that can hold any number of items in a single variable. It can contain different types of values: string, numbers, list etc. The ability to sort the lists is one of the fundamental operations in programming.
Sorting Lists in Python
Python provides two built-in functions that you can use to sort a list: list.sort()
and sorted()
. Both of these methods modify the original list, but they do not return any value - meaning, they do not create a new sorted list as result.
Using built-in functions list.sort(), sorted() and reversed()
First, let’s look at the basic ways to sort lists:
list1 = ['apple', 'banana', 'cherry']
list2 = [3, 1, 4]
list1.sort()
list2.sort(reverse=True)
print(list1, list2)
This will result in: ['apple', 'banana', 'cherry']
and [4, 3, 1]
.
The sorted()
function is similar to the list.sort()
method. It creates a new sorted list from the elements of any sequence:
list1 = ['apple', 'banana', 'cherry']
list2 = [3, 1, 4]
new_list1 = sorted(list1)
new_list2 = sorted(list2, reverse=True)
print(new_list1, new_list2)
This will result in: ['apple', 'banana', 'cherry']
and [4, 3, 1]
.
Keyword Arguments for Sorting Order: reverse, key and lambda
Python allows you to customize the sort order by using keyword arguments with these built-in functions. The reverse
argument can be used to reverse the sorting order.
list1 = ['Apple', 'Cherry', 'Banana']
list2 = [3, 1, 4]
list1.sort(key=str.lower)
list2.sort(reverse=True)
print(list1, list2)
This will result in: ['Apple', 'Banana', 'Cherry']
and [4, 3, 1]
. The key
argument is a function to extract a comparison key from each element. In this case, the comparison keys are the lowercase version of each string.
Advanced sorted functions like sorted()
can also take advantage of these keyword arguments:
list1 = ['Apple', 'Cherry', 'Banana']
new_list1 = sorted(list1, key=str.lower)
new_list2 = sorted(list1, reverse=True)
print(new_list1, new_list2)
This will result in: ['apple', 'banana', 'cherry']
and [4, 3, 1]
.
Advanced Sorted Functions
Python’s built-in sorted function also provides more advanced functionalities like sorting based on multiple keys. You can sort a list of dictionaries based on values in the dictionary and so on:
list_of_dict = [{'name': 'Apple', 'count': 1}, {'name': 'Banana', 'count': 3}, {'name': 'Cherry', 'count': 4}]
new_list = sorted(list_of_dict, key=lambda x: (x['name'].lower(), -x['count']))
print(new_list)
This will result in: [{'name': 'apple', 'count': 1}, {'name': 'banana', 'count': 3}, {'name': 'cherry', 'count': 4}]
. The lambda function creates a tuple as the comparison key, with first element being the lowercase version of name and second being count in descending order.
Conclusion and Further Resources
Sorting lists in Python is easy when you know the built-in functions list.sort() and sorted(). You can customize sorting order by using keyword arguments like reverse
or key
.
You can also use more advanced features such as sorting based on multiple keys, sorting dictionaries etc., with these methods.