Group values and pick the right container by order, uniqueness, key access.
List is a row of cubbies; tuple is the same but you’ve lost the keys; set is a bag (no duplicates); dict is a labelled filing cabinet.
list [1,2,3] ordered, mutable, dup OK
tuple (1,2,3) ordered, immutable, hashable
set {1,2,3} unordered, unique, O(1) `in`
dict {'a':1} insertion-ordered since 3.7
List is your default for ordered mutable sequences. Tuple is the immutable hashable record. Set is for O(1) membership and de-duplication. Dict is the workhorse hashmap (O(1) key lookup). Pick by what you need: don’t list-membership a million items.
fruits = ['apple','banana','apple']
print(len(fruits), fruits.count('apple'))
list keeps duplicates and order; useful for logs.
unique = set(fruits)
'apple' in unique # O(1)
set is O(1) for in; great for de-duplication.
point = (3, 4)
{'x': point[0], 'y': point[1]}
Tuple is hashable, so it can be a dict key (lists cannot).
x in some_list over millions of items in a hot loop - O(n). Use a set or dict (O(1)). Also: tuple records containing mutable lists are ‘immutable’ only at the surface - the list inside can still change.
dict for kv; list for ordered mutation; tuple for fixed records; set for in-checks and dedup. Never use a list as a lookup table on >1000 items.
if x in some_list: over 1M rows is O(n).{row['id']: row for row in rows} or set; O(1) lookup.PERF101; ADR banning in list on >1000 elements.for k in d: ... d.pop(k) ... invalidates the iterator.for k in list(d): ... or build a new dict.