Content · Programming · Python

Python Set Operations on Real Lists of Usernames

Set operations can compare two lists of usernames by first normalizing strings and converting each collection to a set. Use differences to identify new and missing users, and intersection to find shared users. See two.

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

Two access lists need reconciliation after a system migration.

What You Need to Understand


Set operations can compare two lists of usernames by first normalizing strings and converting each collection to a set. Use differences to identify new and missing users, and intersection to find shared users.

When to Use It


Two access lists need reconciliation after a system migration.

Step-by-Step Python Example


current_users = ["Maya", "noa", "liam"]
imported_users = ["maya", "Noa", "avi"]
current = {username.casefold() for username in current_users}
imported = {username.casefold() for username in imported_users}
print("Shared:", sorted(current & imported))
print("Only current:", sorted(current - imported))
print("Only imported:", sorted(imported - current))

How the Example Works


Normalization avoids treating ordinary letter-case differences as different users. Intersections and differences reveal the shared and mismatched identifiers.

Example Output


Shared: ['maya', 'noa']
Only current: ['liam']
Only imported: ['avi']
['a', 'c']

A Second Example


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

before = {"a", "b"}
after = {"b", "c"}
print(sorted(before ^ after))

Important Behavior and Edge Cases


Treat this only as a demonstration. For real identity reconciliation, use stable user IDs, not display names, and validate account identity rather than assuming case folding is universally safe.

Try It Yourself


Add a duplicated username and confirm the unique-membership results do not change.

Common Beginner Mistakes


  • Treat this only as a demonstration. For real identity reconciliation, use stable user IDs, not display names, and validate account identity rather than assuming case folding is universally safe.
  • Applying the example without a clearly defined policy for missing keys, matching identifiers, or validating external values.

Key Takeaways


  • Set operations can compare two lists of usernames by first normalizing strings and converting each collection to a set.
  • Normalization avoids treating ordinary letter-case differences as different users.
  • Treat this only as a demonstration.
Share this resourceSend this page to someone who may find it useful.