In this Python article we want to talk How to Sort & Filter Data with Python Lists, Python is powerful programming language that offers different libraries and functions for managing and manipulating data. one of the most fundamental data structures in Python is list. Lists are ordered collections of elements that can hold any data type including integers, strings and even other lists. Lists are commonly used in data analysis and processing tasks and sorting and filtering are two common operations performed on lists. in this article we want to talk how to sort and filter data with Python lists.

**How to Sort & Filter Data with Python Lists**

Now let’s talk about sorting Python List, Sorting lists involves rearranging the elements of a list in specific order. Python offers several methods for sorting lists.

So first of all let’s talk about sort() method, this method sorts a list in place, it means that it modifies the original list rather than returning new one. sort() method sorts the list in ascending order by default, but you can also specify the reverse parameter to sort the list in descending order. this is an example:

1 2 3 4 5 6 7 8 9 |
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] # Sort in ascending order numbers.sort() print(numbers) # Sort in descending order numbers.sort(reverse=True) print(numbers) |

If you this, you will see this result

Now let’s talk about sorted() function, sorted() function returns new sorted list, and it does not bring changes to the original List. you can also specify reverse parameter to sort the list in descending order. this is an example:

1 2 3 4 5 6 7 8 9 |
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] # Sort in ascending order sorted_numbers = sorted(numbers) print(sorted_numbers) # Sort in ascending order sorted_numbers_desc = sorted(numbers, reverse=True) print(sorted_numbers_desc) |

Run the code and this is the result

Let’s learn that how we can use lambda function with sorting, If you want to sort a list based on specific property of its elements, you can use lambda function to specify the key to sort on. this is an example:

1 2 3 4 |
fruits = ["Python", "Java", "C++", "C#", "Ruby"] sorted_fruits = sorted(fruits, key=lambda fruit: len(fruit)) print(sorted_fruits) |

In this example we have sorted list of programming language based on the length of each programming name.

Run the code and this is the result

We have saw list sorting, let’s talk about Filtering Python List, Filtering lists involves selecting a subset of the elements in a list based on some condition. Python provides several ways to filter lists.

The first way is list comprehension, List comprehensions are the best way to create new lists based on existing ones. you can use list comprehension to filter elements from list based on a condition. this is an example:

1 2 3 4 |
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] even_numbers = [number for number in numbers if number % 2 == 0] print(even_numbers) |

In the above example we have used a list comprehension to create new list that contains only even numbers from the original list of numbers.

This will be the result

Also you can use filter() function, filter() function takes a function and an iterable as arguments and returns new iterable containing only the elements for which the function returns True. this is an example:

1 2 3 4 |
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] even_numbers = list(filter(lambda x: x % 2 == 0, numbers)) print(even_numbers) # [4, 2, 6] |

In the above example we have use filter() function with lambda function to create new list that contains only even numbers from the original list of numbers.

The third way is using for loop, you can also filter elements from a list using a loop and an if statement. this is an example:

1 2 3 4 5 6 7 8 |
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5] even_numbers = [] for number in numbers: if number % 2 == 0: even_numbers.append(number) print(even_numbers) |

In this example we have looped through each element in the original list of numbers and add it to new list if it is even.

This will be the result