Content · Programming · Python

Python Choosing Between Lists, Dictionaries, and Sets

Lists store ordered elements, dictionaries map keys to values, and sets store distinct hashable elements for membership and set operations. Choosing according to the question you need to answer often matters more than.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

A registration system stores users in order, retrieves a user by ID and tracks unique teams.

What You Need to Understand


Lists store ordered elements, dictionaries map keys to values, and sets store distinct hashable elements for membership and set operations. Choosing according to the question you need to answer often matters more than memorizing every method.

When to Use It


A registration system stores users in order, retrieves a user by ID and tracks unique teams.

Step-by-Step Python Example


usernames = ["maya", "noa", "maya"]
profiles = {"maya": "editor", "noa": "viewer"}
unique_users = set(usernames)
print(usernames)
print(profiles["maya"])
print(len(unique_users))

How the Example Works


The list retains the repeated name, the dictionary looks up a role by key, and the set deduplicates names.

Example Output


['maya', 'noa', 'maya']
editor
2
Unknown

A Second Example


Compare this smaller variation with the main example, then experiment with your own values.

users_by_id = {101: "Maya", 102: "Noa"}
print(users_by_id.get(103, "Unknown"))

Important Behavior and Edge Cases


A set is unordered and cannot be indexed. Dictionary keys must be hashable, and a dictionary is not designed to store duplicate keys.

Try It Yourself


Choose a container for a chronological activity log and one for fast lookup by product code.

Common Beginner Mistakes


  • A set is unordered and cannot be indexed. Dictionary keys must be hashable, and a dictionary is not designed to store duplicate keys.
  • Treating missing dictionary keys, replacement precedence, and shallow copies as if they had identical behavior in every context.

Key Takeaways


  • Lists store ordered elements, dictionaries map keys to values, and sets store distinct hashable elements for membership and set operations.
  • The list retains the repeated name, the dictionary looks up a role by key, and the set deduplicates names.
  • A set is unordered and cannot be indexed.
Share this resourceSend this page to someone who may find it useful.