List Properties: first & last
Part of the Logic & Flow section of Coddy's Dart journey — lesson 1 of 65.
When working with lists, you often need to access the first or last element. Instead of using index numbers like [0] or [list.length - 1], Dart provides two convenient properties that make this much simpler.
The .first property gives you the first element in a list, while the .last property gives you the last element. These properties are more readable and less error-prone than calculating index positions.
List<String> planets = ['Mercury', 'Venus', 'Earth', 'Mars'];
String firstPlanet = planets.first; // 'Mercury'
String lastPlanet = planets.last; // 'Mars'These properties work with any non-empty list and automatically handle the index calculations for you. This makes your code cleaner and easier to understand when you need to work with the elements at the beginning or end of your lists.
Challenge
EasyCreate a program that analyzes a music playlist by accessing the first and last songs. Your program should:
- Read a string input representing the playlist name
- Read multiple string inputs representing song titles (the input will end when you receive an empty string)
- Use the
.firstproperty to get the first song in the playlist - Use the
.lastproperty to get the last song in the playlist - Print the playlist analysis in the exact format shown below
For example, if the playlist name is "My Favorites" and the songs are "Bohemian Rhapsody", "Imagine", "Hotel California", your program should output:
Playlist: My Favorites
Total songs: 3
Opening track: Bohemian Rhapsody
Closing track: Hotel CaliforniaYour program must handle playlists with any number of songs and use the .first and .last properties to access the opening and closing tracks.
Cheat sheet
Use .first and .last properties to access the first and last elements of a list:
List<String> planets = ['Mercury', 'Venus', 'Earth', 'Mars'];
String firstPlanet = planets.first; // 'Mercury'
String lastPlanet = planets.last; // 'Mars'These properties are more readable than using index numbers like [0] or [list.length - 1] and work with any non-empty list.
Try it yourself
import 'dart:io';
void main() {
// Read playlist name
String? playlistName = stdin.readLineSync();
// Read songs until empty string
List<String> songs = [];
String? song;
while ((song = stdin.readLineSync()) != null && song!.isNotEmpty) {
songs.add(song);
}
// TODO: Write your code below
// Use songs.first and songs.last to get the opening and closing tracks
// Output the playlist analysis
print("Playlist: $playlistName");
print("Total songs: ${songs.length}");
// Print opening and closing tracks here
}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