Lesson 10 of 12
Keep only the rows that match with Filter Rows, and put them in order with Sort Rows: top 5s, rankings and searches. Run each one, then see it as real code.
About 6 minutes · 3 parts, each with a program to try
Part 1 of 3
A table is a list of rows, and each row has fields: {name: "Arun", marks: 92}.
Filter Rows makes a new list of only the rows where a condition is true, like row.marks > 80. The list it came from stays as it was.
students = [{name: "Arun", marks: 92}, {name: "Priya", marks: 76}, {name: "Kavin", marks: 88}]s in toppersRepeat →
s.nameback to the top for the next lap
ArunKavinfrom types import SimpleNamespace
students = [SimpleNamespace(name="Arun", marks=92), SimpleNamespace(name="Priya", marks=76), SimpleNamespace(name="Kavin", marks=88)]toppers = []for row in students: if row.marks > 80: toppers.append(row)for s in toppers: print(s.name)Part 2 of 3
Sort Rows makes a sorted copy: by one field, like marks, smallest or biggest first.
Fill in Keep the first to keep only the top few: 3 for the top 3.
students = [{name: "Arun", marks: 92}, {name: "Priya", marks: 76}, {name: "Kavin", marks: 88}]s in topRepeat →
s.name + ": " + s.marksback to the top for the next lap
Arun: 92Kavin: 88from types import SimpleNamespace
students = [SimpleNamespace(name="Arun", marks=92), SimpleNamespace(name="Priya", marks=76), SimpleNamespace(name="Kavin", marks=88)]top = sorted(students, key=lambda row: row.marks, reverse=True)[:2]for s in top: print(s.name + ": " + str(s.marks))Part 3 of 3
Filter Rows is a shortcut for a loop you already know: For Each row, a Decision, and Change a List to add it.
This program does the same as the first one, the long way. View Code shows Filter Rows as this loop too.
students = [{name: "Arun", marks: 92}, {name: "Priya", marks: 76}, {name: "Kavin", marks: 88}]toppers = []row in studentsRepeat →
row.marks > 80 true?Yes →
row.name to the end of toppersNo →
nothing, just carry on
back to the top for the next lap
join(toppers)Arun, Kavinfrom types import SimpleNamespace
students = [SimpleNamespace(name="Arun", marks=92), SimpleNamespace(name="Priya", marks=76), SimpleNamespace(name="Kavin", marks=88)]toppers = []for row in students: if row.marks > 80: toppers.append(row.name)print(", ".join(toppers))Quick check