What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Use a list for an ordered collection that can change, a tuple for an ordered group whose item references should stay fixed, a set for unique values and set operations, and a dict to associate keys with values. The right choice depends on whether position, changeability, duplicates, or key-based lookup matters.
How the four Python collection types differ
| Type | Ordering and access | Can the collection change? | Typical use | Key or element rules |
|---|---|---|---|---|
list |
Ordered; access by integer index or slice | Yes | A sequence that changes, such as accumulated results | Lists are unhashable, so they cannot be dictionary keys or set elements |
tuple |
Ordered; access by index or unpacking | No, not at the outer collection level | A fixed group of values, such as a coordinate | Hashable only when all its elements are hashable |
set |
Unordered; supports membership checks, not positional indexing | Yes; frozenset is the immutable counterpart |
Unique values, membership checks, and set operations | Every element must be hashable |
dict |
Retrieve values by key; iteration follows insertion order | Yes | Associating each key with a value | Keys must be hashable and unique |
The Python documentation describes a set as “an unordered collection with no duplicate elements” (Python tutorial, Data Structures). No one type is universally best: choose based on the behavior your code needs.
Use a list for an ordered sequence that changes
Lists use square brackets and preserve positional order:
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →items = ["tea", "coffee"]
items.append("water")
print(items[0]) # tea
Use a list when you need indexing or slicing and expect to add, remove, or replace items. Methods such as append modify the existing list object. If another variable refers to that same object, changes through either name are visible through both; assigning a list to a second name does not, by itself, copy the list.
Popular on Amazon
Use a tuple for a fixed ordered group
Tuples preserve positions like lists, but their own item references cannot be reassigned after creation. Commas make a tuple; parentheses are often used for readability:
point = (3, 4)
x, y = point
single = (3,)
not_a_tuple = (3)
(3) is the integer 3, not a one-item tuple. The comma in (3,) is what makes it a tuple. Tuples are useful for fixed records or grouped values that are naturally unpacked into named variables.
Also popular on Amazon
Immutability does not freeze objects inside a tuple
A tuple cannot have one of its own slots reassigned, but a slot may refer to a mutable object. That inner object can still change:
record = ("colors", ["red", "blue"])
record[1].append("green")
The tuple remains the same tuple, while its contained list changes. This distinction also matters for hashing: a tuple can be a dictionary key or set element only when its contents are hashable. A tuple containing a list is not hashable.
Also popular on Amazon
Use a set for uniqueness and set operations
A set stores unique, hashable elements and does not provide sequence positions. It is useful for checking membership, removing duplicates, and comparing groups of values:
colors = set(["red", "red", "blue"])
warm = {"red", "orange"}
favorites = {"red", "blue"}
print(warm | favorites) # union
print(warm & favorites) # intersection
print(warm - favorites) # in warm, not favorites
print(warm ^ favorites) # in one set, but not both
Do not rely on a set’s displayed or iteration order, and do not try to index it with a number. Use set() to make an empty set: {} creates an empty dictionary instead. For an immutable set, use frozenset.
Also popular on Amazon
Use a dictionary for key-value lookup
A dictionary maps unique, hashable keys to values. Retrieve a value by key, and assign to a key to add or replace its value:
prices = {"tea": 3, "coffee": 4}
print(prices["tea"])
prices["tea"] = 5 # replaces the value for "tea"
Mutable containers such as lists and dictionaries are not hashable, so they cannot be ordinary dictionary keys. Dictionary iteration follows insertion order in current Python. Updating the value for an existing key does not move that key; deleting a key and then inserting it again places it at the end. The language reference identifies insertion order as a language guarantee starting with Python 3.7 (Python language reference, Data model).
Also popular on Amazon
Hashability: why it matters for keys and set elements
A hashable object has a hash value that remains stable during its lifetime and can be compared with other objects. Dictionaries use hashable objects as keys; sets use them as elements. This is why a list cannot be a key or set member, and why a tuple qualifies only if its contents qualify too. See the Python glossary definition of hashable for the general rule.
Quick Recap
Common mistakes to avoid
- Expecting a set to be sorted: sets are unordered; use a sequence if positional order matters.
- Using
{}for an empty set: writeset(); braces alone create an empty dictionary. - Writing
(item)for a one-item tuple: write(item,). - Assuming every tuple can be a key: all of its elements must be hashable.
- Trying to use a list as a dictionary key: lists are mutable and unhashable.
- Treating tuple immutability as deep immutability: a mutable object inside a tuple can still change.
- Indexing a set: sets have no stable numeric positions.
A quick decision guide
- Choose a
listwhen order and positional access matter, and the collection may change. - Choose a
tuplewhen you need an ordered group whose outer structure should remain fixed. - Choose a
setwhen values must be unique, membership matters, or you need union, intersection, difference, or symmetric difference. - Choose a
dictwhen each item should be found through a meaningful key.

