When do you choose a list, tuple, set or dict?
BeginnerConceptLanguage essentials & functions
Answer
- list: ordered, mutable sequence; a list of instance IDs to process.
- tuple: ordered, immutable; fixed records like
(host, port)or dict keys made of several fields. - set: unordered, unique, O(1) membership; "which required tags are missing" is
required - set(tags). - dict: key to value mapping with O(1) lookup, insertion-ordered since 3.7; tags, config, lookups by ID.
Choosing a set instead of a list for membership checks turns an O(n²) audit loop into O(n).
More on Language essentials & functions
Why is Python the default scripting language in so many DevOps teams, and where would you not use it?BeginnerWhat is the difference between mutable and immutable types? Give examples.BeginnerWhat does assignment do in Python, and how do shallow and deep copies differ?BeginnerExplain the mutable default argument bug and how to fix it.IntermediateWhat is the difference between `==` and `is`?BeginnerWhat values are "falsy" in Python, and why does it matter in scripts?Beginner