Skip to main content

Menu

System onlineAI Tutorials / 2026

AI Tutorials
Knowledge search

Recent · Popular · Tutorials

  • dart3 tutorials
  • java1 tutorial
  • Javascript3 tutorials
  • Node.js Introduction2 tutorials
  • dartdart · General · In depth
  • JavascriptJavascript · Advanced · In depth
  • JavascriptJavascript · Advanced · Mid
  • JavascriptJavascript · Advanced · Short

Find a tutorial

Tell the assistant what you want to learn. It suggests published chapters.

For example: “I want to learn Rust ownership from the start.” The conversation is kept only in this tab.

0 / 2,000 characters

Enter to send, Shift+Enter for a new line. Do not include personal information.

Chapter 7

Working with Lists, Sets, and Maps

Real programs handle many values at once: the items in a cart, the users logged in, the settings for an app. Dart gives you three main containers for this, called collections. A **List** keeps values in order. A **Set** keeps each value only once. A **Map** links keys to values, like a dictionary li
Level: GeneralLength: Mid2 min read

Read at another depth

Working with Lists, Sets, and Maps

Real programs handle many values at once: the items in a cart, the users logged in, the settings for an app. Dart gives you three main containers for this, called collections. A List keeps values in order. A Set keeps each value only once. A Map links keys to values, like a dictionary links words to definitions.

Lists: Ordered Values

A List is a numbered sequence. The first item is at position 0, the second at position 1, and so on.

Code example in dart
void main() {
  final fruits = ['apple', 'banana', 'cherry'];
  print(fruits[0]);      // apple
  print(fruits.length);  // 3
  fruits.add('date');    // add at the end
  fruits.remove('banana');
  print(fruits);         // [apple, cherry, date]
}

You can also reach items from the end: fruits.last is the final item, and fruits.first is the first.

Sets: Each Value Once

A Set stores values with no duplicates and no fixed order. Adding a value that is already there does nothing.

Code example in dart
void main() {
  final tags = {'dart', 'tutorial'};
  tags.add('dart');        // ignored, already present
  tags.add('beginner');
  print(tags.contains('dart'));  // true
  print(tags.length);            // 2
}

A common trick: convert a List to a Set to remove duplicates.

Code example in dart
final messy = ['a', 'b', 'a', 'c'];
final unique = messy.toSet().toList();  // [a, b, c]

Maps: Keys and Values

A Map pairs each key with one value. You look things up by key, not by position.

Code example in dart
void main() {
  final ages = {'Ada': 36, 'Alan': 41};
  print(ages['Ada']);        // 36
  ages['Grace'] = 45;        // add a new pair
  ages['Alan'] = 42;         // update an existing value

  for (final entry in ages.entries) {
    print('${entry.key} is ${entry.value}');
  }
}

If a key is missing, ages['Bob'] returns null rather than crashing. This connects directly to the null safety ideas from earlier chapters.

Picking the Right Collection

Choosing well makes your code simpler and faster.

NeedUseWhy
Ordered items, duplicates allowedListIndex access, keeps order
Unique valuesSetFast membership checks
Lookup by name or IDMapInstant find-by-key

callout tip If you find yourself checking "is this value already in my List?" in a loop, you probably wanted a Set. The contains check on a Set is fast even for large collections.

What Comes Next

Collections become far more powerful when you transform them: turn every item into something else, keep only some items, or boil a list down to a single value. That is the subject of the next chapter, where we meet map, where, and reduce.

Exercise

You are storing the email addresses of people who signed up, and you must never store the same email twice. Which collection should you use, and why?

Show answer

A Set. It automatically refuses duplicates, so adding the same email twice stores it only once, and checking whether an email is already present is fast.

Exercise

What does ages['Bob'] return if the map ages has no 'Bob' key?

Show answer

It returns null. Dart does not crash on a missing Map key; it gives you null, which you can handle with null-safe operators like ??.

Spotted a mistake, or code that does not work?

Report an issue

Chapter: Working with Lists, Sets, and Maps

0 / 2,000 characters

Plain text, for example: the second code sample does not compile. Do not include personal information.

You can't do that