Reversing a List
Part of the Logic & Flow section of Coddy's Dart journey — lesson 3 of 65.
Sometimes you need to view the elements of a list in reverse order. Dart provides the .reversed property that creates a reversed view of your list without changing the original list.
The .reversed property returns an iterable that presents the list elements in reverse order. This means the last element appears first, the second-to-last appears second, and so on.
List<int> numbers = [1, 2, 3, 4, 5];
var reversedNumbers = numbers.reversed;
print(reversedNumbers); // (5, 4, 3, 2, 1)
// Original list remains unchanged
print(numbers); // [1, 2, 3, 4, 5]An important detail is that .reversed returns an iterable, not a list. If you need to work with it as a list, you can convert it using .toList(). This property is particularly useful when you need to process data in reverse order while keeping your original list intact.
Challenge
EasyCreate a program that analyzes a reading list by displaying books in both their original and reversed order. Your program should:
- Read a string input representing the reading list title
- Read multiple string inputs representing book titles (the input will end when you receive an empty string)
- Use the
.reversedproperty to create a reversed view of the book list - Print the reading list analysis in the exact format shown below
For example, if the reading list title is "Summer Reading" and the books are "1984", "To Kill a Mockingbird", "The Great Gatsby", your program should output:
Reading List: Summer Reading
Books in original order: (1984, To Kill a Mockingbird, The Great Gatsby)
Books in reversed order: (To Kill a Mockingbird, The Great Gatsby, 1984)
Total books: 3If the reading list title is "Quick Reads" and only one book "Animal Farm" is provided, your program should output:
Reading List: Quick Reads
Books in original order: (Animal Farm)
Books in reversed order: (Animal Farm)
Total books: 1Your program must use the .reversed property to display the books in reverse order. Remember that .reversed returns an iterable, so when you print it, it will display with parentheses rather than square brackets.
Cheat sheet
Use the .reversed property to view list elements in reverse order without modifying the original list:
List<int> numbers = [1, 2, 3, 4, 5];
var reversedNumbers = numbers.reversed;
print(reversedNumbers); // (5, 4, 3, 2, 1)
// Original list remains unchanged
print(numbers); // [1, 2, 3, 4, 5]The .reversed property returns an iterable, not a list. To convert it back to a list, use .toList().
Try it yourself
import 'dart:io';
void main() {
// Read the reading list title
String? title = stdin.readLineSync();
// Read book titles until empty string
List<String> books = [];
String? book;
while ((book = stdin.readLineSync()) != null && book!.isNotEmpty) {
books.add(book);
}
// TODO: Write your code here
// Use the .reversed property to create a reversed view of the books list
// Print the output in the required format
}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