List Removal: removeWhere
Part of the Logic & Flow section of Coddy's Dart journey — lesson 5 of 65.
Sometimes you need to remove multiple elements from a list based on a specific condition. Instead of removing elements one by one, Dart provides the removeWhere() method that can remove all elements that match a test condition in a single operation.
The removeWhere() method takes a test function as its parameter. This function receives each element in the list and returns true if the element should be removed, or false if it should stay. The method then removes all elements for which the test function returns true.
List<int> numbers = [1, 2, 3, 4, 5, 6, 7, 8];
// Remove all even numbers
numbers.removeWhere((number) => number % 2 == 0);
print(numbers); // [1, 3, 5, 7]This approach is much more efficient than manually searching for and removing elements individually. The removeWhere() method modifies the original list directly, making it a powerful tool for cleaning up lists based on any condition you can express as a boolean test.
Challenge
EasyCreate a program that manages a student grade filtering system by removing students who failed from a class roster. Your program should:
- Read a string input representing the class name
- Read multiple integer inputs representing student scores (the input will end when you receive
-1) - Use the
removeWhere()method to remove all scores that are below 60 (failing grades) - Print the grade filtering results in the exact format shown below
For example, if the class name is "Mathematics 101" and the scores are 85, 45, 92, 38, 76, 55, 88, your program should output:
Class: Mathematics 101
Original scores: [85, 45, 92, 38, 76, 55, 88]
Removed failing scores (below 60)
Passing scores: [85, 92, 76, 88]
Students passed: 4
Students failed: 3If the class name is "History 201" and all scores are passing: 75, 82, 91, your program should output:
Class: History 201
Original scores: [75, 82, 91]
Removed failing scores (below 60)
Passing scores: [75, 82, 91]
Students passed: 3
Students failed: 0If the class name is "Chemistry 301" and all scores are failing: 45, 32, 58, your program should output:
Class: Chemistry 301
Original scores: [45, 32, 58]
Removed failing scores (below 60)
Passing scores: []
Students passed: 0
Students failed: 3Your program must use the removeWhere() method with a test function that returns true for scores below 60. The method should modify the original list by removing all failing grades, leaving only the passing scores.
Cheat sheet
The removeWhere() method removes all elements from a list that match a specific condition. It takes a test function that returns true for elements to be removed and false for elements to keep.
List<int> numbers = [1, 2, 3, 4, 5, 6, 7, 8];
// Remove all even numbers
numbers.removeWhere((number) => number % 2 == 0);
print(numbers); // [1, 3, 5, 7]The method modifies the original list directly and is more efficient than removing elements individually.
Try it yourself
import 'dart:io';
void main() {
// Read class name
String? className = stdin.readLineSync();
// Read scores until -1 is encountered
List<int> scores = [];
while (true) {
int score = int.parse(stdin.readLineSync()!);
if (score == -1) {
break;
}
scores.add(score);
}
// Store original scores for display
List<int> originalScores = List.from(scores);
// TODO: Write your code below
// Use removeWhere() method to remove scores below 60
// Print the results
print('Class: $className');
print('Original scores: $originalScores');
print('Removed failing scores (below 60)');
print('Passing scores: $scores');
print('Students passed: ${scores.length}');
print('Students failed: ${originalScores.length - scores.length}');
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced List Manipulation
List Properties: first & lastList State: isEmpty & isNotEmpReversing a ListAdding to a List: insertList Removal: removeWhereFinding in a List: indexOfSorting a ListShuffling a ListRecap - List Organizer4Advanced Map Manipulation
Iterating Over a MapChecking for Keys and ValuesMap Properties: keys & valuesConditional Add: putIfAbsentRemoving Entries from a MapNested MapsRecap - Inventory Update2Functional List Operations
Transforming with 'map'Filtering with 'where'Using '.toList()'Checking Conditions with 'any'Conditions with 'every'Finding with 'firstWhere'Recap - Data Filtering5Project: Shopping Cart Calc
Project SetupAdding Items to the Cart3Sets
What is a Set?Creating a SetAdding and Removing from SetsChecking for Elements in a SetConverting a List to a SetSet UnionSet IntersectionSet DifferenceRecap - Unique Guest List