Python provides several built-in collection types, and choosing the right one makes code easier to read and often more efficient. Lists preserve an ordered sequence that can change, tuples represent an ordered collection that is generally used as a fixed record, sets store unique hashable elements, and dictionaries map keys to values.
Use a list for an ordered, changeable sequence
A list preserves the order of its items and supports adding, removing and replacing elements. It is a good choice for tasks, scores, names or any sequence that may change as a program runs.
List indexes start at zero, and indexing beyond the available range raises IndexError. If you only need to visit every item, direct iteration often reads more clearly than a manual index loop.
courses = ['Math', 'Statistics', 'Python']
courses.append('SQL')
print(courses[0]) # MathUse a tuple for a fixed record or returned group of values
A tuple is ordered and immutable: the tuple's item references cannot be reassigned after creation. It can be useful for a coordinate, a date-like record or a function returning several related values.
A tuple is not deeply immutable when it contains mutable objects. For example, a tuple can hold a list that is then modified. Use immutability carefully and choose a named data structure when named fields improve clarity.
point = (10, 25)
x, y = point
print(x, y)Use a set for unique values and membership checks
A set stores unique hashable elements. It is useful for removing duplicates from a collection, checking membership or comparing groups. Sets do not provide an index-based sequence, so do not rely on a particular display order.
Values must be hashable to be placed in a set, which means lists and dictionaries cannot be set elements. Immutable values such as strings and integers are commonly used.
subjects = {'math', 'python', 'math'}
print(subjects) # one 'math' valueUse a dictionary to map keys to values
A dictionary stores key-value pairs. It is useful when a value should be looked up by a meaningful key, such as a student's ID mapped to a name or a product code mapped to a price. Dictionary keys must be hashable and unique within the mapping.
Modern Python dictionaries preserve insertion order, but a dictionary is still best understood as a mapping rather than an index-based sequence.
credits = {'Math': 3, 'Python': 4}
print(credits['Python']) # 4Comparison table and selection guide
Think first about the operation you need. If you need order and frequent edits, choose a list. If you need a small fixed grouping, a tuple may be appropriate. If uniqueness is the priority, use a set. If every item has a key and a value, use a dictionary.
Performance depends on the operation and data shape, so avoid choosing a type based only on a generalized speed claim. Select the structure that expresses the model clearly, then measure if performance becomes important.
- List: ordered, mutable sequence; duplicates allowed.
- Tuple: ordered, immutable sequence of references; duplicates allowed.
- Set: unique hashable elements; no indexing.
- Dictionary: unique keys mapped to values; lookup by key.
- Choose based on the data relationship and operations you need.
Understanding the concept is the first step.
Explore related resources or learn about subject-focused tutoring designed to help you understand the material.
