In this Python article we want to learn about Data Filtering with Python Control Structures, so Data filtering is an important technique used in data analysis, because using this technique you extract a subset of data that meets specific set of conditions. Python provides different control structures that you can use for filtering data. In this article we want to talk that how to filter data with Python control structures.
Data Filtering with Python Control Structures
Before starting our main topic, let’s talk about data filtering, so data filtering involves selecting a subset of data that meets specific criteria. this criteria can be any condition, such as a range of values, a pattern, or specific value.
For example you have a dataset of customer reviews for a product, and now you want to filter all the reviews with the rating less than 4. for doing this you need tp apply a filter that selects only the rows with a rating greater than or equal to 4, in Python there are different control structures that you can use to apply data filters, for example we have if statements, for loops and list comprehensions.
Now let’s talk about filtering data with if statement, if statement is a basic control structure in Python that allows you to check whether a condition is true or false. you can use if statement to apply a filter to a dataset by checking each row and selecting only the rows that meets a specific condition.
For example, let’s say we have a list of numbers, and we want to filter all the numbers that are less than 5. we can do this using this code:
1 2 3 4 5 6 7 8 9 |
numbers = [1, 3, 5, 7, 9, 2, 4, 6, 8] filtered_numbers = [] for num in numbers: if num >= 5: filtered_numbers.append(num) print(filtered_numbers) |
In the above code, we have defined a list of numbers. after that we have created an empty list called filtered_numbers, we use that to store the numbers that meets our filtering condition.
After that we have used a for loop to iterate over each number in the numbers list. for each number we checks if it is greater than or equal to 5 using an if statement. if the number meets the condition we append it to the filtered_numbers list. and lastly we print out the filtered_numbers list, which contains only the numbers that are greater than or equal to 5.
Run the code and this will be the result

List comprehensions is another way for creating lists in Python, you can also use list comprehensions for filtering data, because they allows you to apply a filter to a dataset in single line of code.
For example we have a list of numbers, and we want to filter all numbers that are less than 5. we can do this using list comprehension like this.
1 2 3 4 5 |
numbers = [1, 3, 5, 7, 9, 2, 4, 6, 8] filtered_numbers = [num for num in numbers if num >= 5] print(filtered_numbers) |
If you run the code you will see the same result.
Learn More
- Python Control Structures for Decision Making
- Iteration and Looping in Python Control Structures
- Python Control Structures for Conditional Statements
- Python Control Structure for Handling Exception