Choosing the Right Collection
Pick the structure that fits the job.
Choosing the Right Collection is a free Mojo Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Three Tools, One Choice
List, Dict, and Set each shine for a different job. Picking the right one makes your code clearer and faster. 🧰
Order Matters? Use a List
When sequence and position count, choose a List. It keeps insertion order and lets you index by number.
Lookup by Name? Use a Dict
When you find values by a key rather than a position, a Dict fits. It maps names to data for fast lookup.
Uniqueness Only? Use a Set
When you just need each item once or quick membership checks, pick a Set. It dedupes automatically.
Duplicates Allowed?
List and Dict values can repeat, but a Set keeps only one copy of each member. Let that guide your choice.
Speed of Membership
Asking is x in here is slow for a List but fast for a Set or Dict. Frequent checks favor those two.
Indexing Behavior
Only a List is indexed by integer position. A Dict is indexed by key, and a Set has no indexing at all.
Counting Occurrences
Need to count how many times each item appears? Pair items with counts in a Dict of item to number.
var counts = Dict[String, Int]()Combine Them Freely
Real programs mix collections, like a Dict whose values are Lists. Compose the structure your data needs.
var groups = Dict[String, List[Int]]()Start Simple
If unsure, begin with a List and switch later. Choosing well is about fit, not picking the fanciest option.
A Quick Mental Model
Think: ordered means List, keyed means Dict, unique means Set. That one line settles most decisions.
Quick Check
You must repeatedly check whether an item is present and you do not care about order. Which collection is best?
Recap
Match the collection to the job: List for order, Dict for keyed lookup, Set for uniqueness. 🎯
Frequently asked questions
Is the “Choosing the Right Collection” lesson free?
Yes — the full text of “Choosing the Right Collection” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.
What will I learn in “Choosing the Right Collection”?
Pick the structure that fits the job. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Mojo Academy?
No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Choosing the Right Collection” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Mojo Academy lesson?
Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Building and Growing a List
- Key-Value Data with Dict
- Unique Items with Set
- Choosing the Right Collection