How to Sort & Filter Data with Python Lists

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:

 

 

 

If you this, you will see this result

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

 

 

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: 

 

 

 

Run the code and this is the result

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

 

 

 

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:

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

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

 

 

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:

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

Python List Filtering
Python List Filtering

 

 

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:

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:

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

Python Filtering with For Loop
Python Filtering with For Loop

 

 

 

Learn More on Python

Leave a Comment