How to filter data in Python?

Coding in Python

Filtering data in Python can be done using various methods. Here’s an example using the built-in filter. For example:

VBA Code Display
# Sample list of dictionaries representing items with a 'category' key
items = [
    {'name': 'Car1', 'category': 'car'},
    {'name': 'Bike1', 'category': 'bike'},
    {'name': 'Car2', 'category': 'car'},
    {'name': 'Truck1', 'category': 'truck'},
    ]
            
# Define a filter condition using a lambda function
filtered_cars = list(filter(lambda item: item['category'] == 'car', items))
            
# Print the filtered result
print(filtered_cars)
Scroll to Top