Menu
Coddy logo textTech

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 icon

Challenge

Easy

Create a program that analyzes a reading list by displaying books in both their original and reversed order. Your program should:

  1. Read a string input representing the reading list title
  2. Read multiple string inputs representing book titles (the input will end when you receive an empty string)
  3. Use the .reversed property to create a reversed view of the book list
  4. 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: 3

If 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: 1

Your 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
  
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow