USD ($)
$
United States Dollar
Euro Member Countries
India Rupee
د.إ
United Arab Emirates dirham
ر.س
Saudi Arabia Riyal

Choosing the Right Data Structure

Lesson 17/35 | Study Time: 35 Min

Python gives you four built-in data structures — lists, tuples, dictionaries, and sets — and each one exists for a reason. Choosing the wrong one does not always break your code, but it can make it slower, harder to read, and more prone to errors.

Knowing which structure fits which situation is a practical skill that improves both the quality and efficiency of your programs, especially when working with large datasets in AI.

Use a List 

A list is the right choice when you need an ordered collection that will change over time.


1. You need to add, remove, or update items.

2. Order matters and must be preserved.

3. Duplicate values are acceptable.

Common AI use cases:


1. Storing a sequence of training data samples.

2. Collecting model predictions.

3. Building a list of features for processing.

Use a Tuple When

A tuple is the right choice when the data should not change after it is created.


1. The values are fixed and must be protected.

2. You need a slight performance advantage over lists.

3. You want to use the collection as a dictionary key.

Common AI use cases:


1. Storing fixed model input shapes.

2. Representing coordinate pairs or RGB values.

3. Returning multiple results from a function.

Use a Dictionary When

A dictionary is the right choice when data has a name or label attached to it.


1. You need to look up values using a meaningful key.

2. Data is structured with multiple attributes.

3. You are working with JSON-like or record-style data.


Common AI use cases:


1. Storing model hyperparameters.

2. Mapping class labels to category names.

3. Handling API responses and JSON data.

Use a Set When

A set is the right choice when uniqueness is the priority and order does not matter.


1. You need to remove duplicates automatically.

2. You want to compare two collections.

3. Fast membership checking is needed.


Common AI use cases:


1. Removing duplicate labels or tags.

2. Comparing feature sets across datasets.

3. Checking if a category exists in a collection.

Choosing the Right Structure

Use this as a quick reference when deciding:

Side-by-Side Comparison Example

The same data stored in different structures shows how the choice affects access and usage:

The dictionary version is clearly the most readable and practical for structured records.

Common Mistakes to Avoid


1. Using a list when you only need unique values — use a set instead.

2. Using a list for fixed data that never changes — use a tuple for safety and speed.

3. Using a list when you need labelled access — use a dictionary for clarity.

4. Creating an empty set with {} — this creates a dictionary; use set() instead.

Sales Campaign

Sales Campaign

We have a sales campaign on our promoted courses and products. You can purchase 1 products at a discounted price up to 15% discount.