Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Sorting and Filtering Data | Organizing and Summarizing Data
Python for Daily Tasks

bookSorting and Filtering Data

Deslize para mostrar o menu

When you want to organize information in Python, sorting and filtering are two essential techniques. Sorting helps you arrange items in a particular order, such as placing your most urgent tasks at the top of your to-do list. Filtering, on the other hand, allows you to pick out only the items that match certain criteria, like finding all expenses above a certain amount. The sorted() function and list comprehensions are two powerful tools that make these tasks quick and easy.

123456789101112
# Suppose you have a list of tasks, each with a priority value. tasks = [ {"task": "Finish report", "priority": 2}, {"task": "Buy groceries", "priority": 3}, {"task": "Pay bills", "priority": 1} ] # Sort the tasks by their priority (lowest number = highest priority) sorted_tasks = sorted(tasks, key=lambda x: x["priority"]) for t in sorted_tasks: print(f'{t["task"]} (Priority {t["priority"]})')
copy

Filtering is just as useful as sorting. With filtering, you can create a new list that contains only the items you care about. For example, if you want to see which of your expenses are above $50, you can use a list comprehension to quickly get that information.

1234567
# List of expenses in dollars expenses = [23.50, 75.00, 19.99, 120.00, 45.00] # Find all expenses above $50 large_expenses = [expense for expense in expenses if expense > 50] print("Expenses above $50:", large_expenses)
copy

1. What does the sorted() function return?

2. How can you filter a list to include only items that meet a condition?

3. Fill in the blanks to filter a list for values greater than a threshold.

question mark

What does the sorted() function return?

Select the correct answer

question mark

How can you filter a list to include only items that meet a condition?

Select the correct answer

question-icon

Fill in the blanks to filter a list for values greater than a threshold.

for in numbers if > threshold
[12, 18]

Clique ou arraste solte itens e preencha os espaços

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 6

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Seção 2. Capítulo 6
some-alt