Demystified: Tuple vs. Dict
The real distinction between tuples and dicts, and how it relates to Dependency Injection, Joining of SQL Tables and more…
Technically, tuples and dicts are equivalent in terms of content: Tuples select the feature by positional index, while dicts select by a unique identifier called key.
Note: In this article, I prefer to use tuple and dict as in Python, since there is no universal name for both. If you come from a Go background, it’s struct and map respectively.
Table of Contents
1.1: Information is Stored as Tuple by Default
1.2: Tuples Can Be Compared Among Themselves
1.3: Tuples Cannot Interact With Other Types of Tuples
2.1: Dependency Injection: What, How, Why?
2.2: Mocking Using Dependency Injection
2.3: How Dependency Injection Relates to Tuples vs. Dicts
3: Step-by-Step Construction of Dicts in Maths: Multiset, Ordered Set, Abstract Tuple, Dict
4.1: Dependency Injection on Person-Animal Comparison
4.2: How Merging Two Tuples Would Work Using Dicts
4.3: How Tuple vs. Dict Relationship Is Related to Joining Datasets
Information is Stored as Tuple by Default
Unlike dicts, the features of tuples are strictly defined, and also their domains are known, which is why tuples are much safer to use.
Before looking up a feature from a tuple, you need to first make sure that the tuple contains this feature and the position of feature is known.
Tuples Can Be Compared Among Themselves
With this in mind, tuples of same kind can be compared and sorted based on their features. For example:
- Let’s define a person signature as follows: P_i = (a_i, w_i, h_i) where a_i is “age”, w_i is “weight” and h_i is “height”.