Dart – Reverse a List
To reverse a list in Dart, use list.reversed.toList(). The reversed property returns an Iterable that reads the elements from last to first, and toList() converts that iterable into a new list.
void main() {
final numbers = [24, 56, 84, 92];
final reversedNumbers = numbers.reversed.toList();
print(reversedNumbers);
}
Output
[92, 84, 56, 24]
This operation leaves the original list unchanged. When the original list itself must be modified, its elements can instead be swapped from both ends toward the middle.
What Happens When a Dart List Is Reversed?
Reversing a list changes the order in which its elements appear. The last element becomes the first, the second-to-last element becomes the second, and the original first element becomes the last.
For example, reversing [10, 20, 30, 40] produces [40, 30, 20, 10]. The values themselves are not changed.
reversed.toList()creates a separate list in reverse order.- A swapping loop reverses the existing mutable list in place.
- A reverse-index loop can build a new list manually.
Reverse a Dart List with reversed.toList()
The most direct modern Dart expression for creating a reversed list is list.reversed.toList().
The reversed property does not itself return a List. It returns an Iterable, so call toList() when list operations, indexed access, or a separate list object is required.
void main() {
final names = ['Amir', 'Beena', 'Charan'];
final reversedNames = names.reversed.toList();
print('Original: $names');
print('Reversed: $reversedNames');
}
Output
Original: [Amir, Beena, Charan]
Reversed: [Charan, Beena, Amir]
The output confirms that the original names list keeps its initial order.
Reverse Dart List using List.reversed
List.reversed returns an Iterable of the objects in this list in reverse order. We can use this iterable to initialize a new List.
In the following Dart program, we take a list and initialize a new list with the constructor new List.from() and pass the reversed list iterable as argument to the constructor.
Dart Program
void main(){
//a list
var myList = [24, 56, 84, 92];
//intialize a new list from iterable to the items of reversed order
var reversedList = new List.from(myList.reversed);
print(reversedList);
}
Output
D:\tutorialkart\workspace\dart_tutorial>dart example.dart
[92, 84, 56, 24]
The elements of the list are reversed.
In current Dart code, the same operation is commonly written with myList.reversed.toList() or List.from(myList.reversed). The optional new keyword is no longer normally used.
Reverse the Original Dart List In Place
Dart’s List class does not provide a method named reverse() that mutates the list. To reverse the same list object, swap the first and last elements, then the second and second-to-last elements, continuing until the middle is reached.
The following reusable function modifies a mutable list in place:
void reverseInPlace<T>(List<T> list) {
for (var left = 0, right = list.length - 1;
left < right;
left++, right--) {
final temporary = list[left];
list[left] = list[right];
list[right] = temporary;
}
}
void main() {
final numbers = [1, 2, 3, 4, 5];
reverseInPlace(numbers);
print(numbers);
}
Output
[5, 4, 3, 2, 1]
Only half of the list needs to be visited because every iteration places two elements in their reversed positions.
Reverse Dart List in-place by Swapping in For Loop
We can use a for loop to iterate till the middle of the list and for each iteration we swap the element at the index with the element at the N-1-index.
As we doing this in the original list, the original list will finally have the reversed list. Hence, this is called reversing a Dart List in-place.
In the following Dart Program, we shall reverse a list in-place using for loop and swapping.
Dart Program
void main(){
var myList = [24, 56, 84, 92];
for(var i=0;i<myList.length/2;i++){
var temp = myList[i];
myList[i] = myList[myList.length-1-i];
myList[myList.length-1-i] = temp;
}
print(myList);
}
Output
D:\tutorialkart\workspace\dart_tutorial>dart example.dart
[92, 84, 56, 24]
The original list, after execution of for loop, contains the reversed list.
Build a Reversed Dart List with Reverse Indexes
A reversed copy can also be constructed manually by reading indexes from length - 1 down to zero. This approach is useful when learning how list indexing works or when additional processing must be applied while copying each element.
void main() {
final original = [3, 6, 9, 12];
final reversed = <int>[];
for (var index = original.length - 1; index >= 0; index--) {
reversed.add(original[index]);
}
print(reversed);
}
Output
[12, 9, 6, 3]
Reverse Dart List with Reversed List as a new List and Saving Original
Also, we can reverse a list in some primitive way. Where we create an empty list of size as that of original list, and copy the elements one by one from the original list from the end to the new list from starting.
Dart Program
void main(){
var myList = [24, 56, 84, 92];
var reversedList = new List(myList.length);
for(var i=0;i<myList.length;i++){
reversedList[i] = myList[myList.length-1-i];
}
print(reversedList);
}
Output
D:\tutorialkart\workspace\dart_tutorial>dart example.dart
[92, 84, 56, 24]
The example above shows an older Dart list-construction style. In null-safe Dart, prefer a growable typed list with add(), as in the reverse-index example, or use List.generate().
Reverse a Dart List with List.generate()
List.generate() can create a reversed copy by calculating the source index for each position in the new list.
void main() {
final letters = ['a', 'b', 'c', 'd'];
final reversedLetters = List<String>.generate(
letters.length,
(index) => letters[letters.length - 1 - index],
);
print(reversedLetters);
}
Output
[d, c, b, a]
Reverse Only Part of a Dart List
To reverse a selected range while preserving the rest of the list, combine sublist(), reversed, and replaceRange(). In this example, indexes 1 through 3 are reversed.
void main() {
final values = [10, 20, 30, 40, 50];
const start = 1;
const end = 4;
values.replaceRange(
start,
end,
values.sublist(start, end).reversed,
);
print(values);
}
Output
[10, 40, 30, 20, 50]
The end index is exclusive, so the reversed range contains the elements at indexes 1, 2, and 3.
Reversing Empty, Single-Element, and Unmodifiable Lists
An empty list remains empty after reversal, and a one-element list keeps the same order. No special condition is needed when using reversed.toList().
void main() {
print(<int>[].reversed.toList());
print([7].reversed.toList());
}
Output
[]
[7]
An unmodifiable list cannot be reversed in place because its indexed values cannot be assigned. However, reversed.toList() can still create a new mutable reversed list from it.
Reversed Copy Versus In-Place Dart List Reversal
- Use
list.reversed.toList()when the original order must be preserved. - Use an element-swapping loop when the same mutable list should be changed.
- Use a reverse-index loop or
List.generate()when each copied element also needs custom processing. - Use
replaceRange()when only a specific section of the list should be reversed.
Both a reversed copy and an in-place reversal take linear time because every element must be visited. Creating a reversed copy also requires storage for the new list, while swapping changes the existing list with only a temporary value.
Common Questions About Reversing Dart Lists
Does list.reversed change the original Dart list?
No. The reversed property provides the elements in reverse iteration order. Calling toList() creates a separate list, while the original list retains its order.
Why does reversed return an Iterable instead of a List?
An Iterable represents a sequence that can be traversed without requiring an immediate list copy. Convert it with toList() when a concrete list is needed.
How do I reverse a Dart list without creating another list?
Use an in-place swapping loop. Swap elements at the left and right indexes, move both indexes toward the center, and stop when they meet.
Can an unmodifiable Dart list be reversed?
It cannot be modified in place. You can still produce a reversed copy with unmodifiableList.reversed.toList().
Dart List Reversal Review Checklist
- Confirm whether the original list must remain unchanged.
- Convert the reversed iterable with
toList()when a list result is required. - Use a mutable list before attempting indexed swaps or
replaceRange(). - Check that reverse-index loops start at
length - 1and include index zero. - Test even-length, odd-length, empty, and single-element lists when maintaining custom reversal code.
Conclusion
In this Dart Tutorial, we learned how to reverse a list using List.reversed, for loop with swapping, and a primitive way where we copy elements from original to reversed.
For most Dart programs, list.reversed.toList() is the clearest way to create a reversed copy. Use element swapping when the original mutable list itself must be reversed.
TutorialKart.com