Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes
Sponsored Reserved space — layout preview until AdSense is connected
Python

Python Lists vs Sets vs Dictionaries: Which One Should You Use?

Learn when to use Python lists, sets, and dictionaries with real examples and performance comparisons. This guide helps you choose the right data structure for faster, cleaner code.

July 2026 8 min read 1 views 0 hearts

You're writing Python code, and you need to store some data. The first thing that pops into your head is probably a list. But is that always the best choice? Not really. Python gives you three powerful tools for storing collections: lists, sets, and dictionaries. Each one has its own superpower, and picking the wrong one can make your code slower or harder to work with.

Let me break down when to use each one, with real examples you'll actually encounter.

The Quick Cheat Sheet

Before we dive deep, here's the one-sentence summary:

  • Lists are for ordered sequences where you might need duplicates
  • Sets are for unique items where order doesn't matter
  • Dictionaries are for mapping keys to values

Now let's see why this matters in practice.

Lists: Your Go-To for Ordered Data

Lists are the workhorses of Python. You use them when the order of items matters and you might have duplicates.

Real example from PythonSkillset: Imagine you're building a to-do list app. Tasks come in a specific order, and you might have two tasks that say "Buy milk" (because you keep forgetting). A list is perfect here.

todo_list = ["Buy milk", "Call dentist", "Buy milk", "Finish report"]
print(todo_list[0])  # "Buy milk" - order preserved

Lists shine when you need: - Index-based access (like my_list[2]) - Duplicate values - Maintaining insertion order - Modifying items in place

But here's the catch: checking if an item exists in a list is slow for large lists. Python has to scan through every element. If you're doing if "something" in my_list on a list with 10,000 items, you'll feel the lag.

Sets: When Uniqueness Matters

Sets are like lists that automatically remove duplicates. They're also blazingly fast for membership tests.

Real example from PythonSkillset: You're building a system that tracks unique visitors to a website. Every time someone visits, you add their IP address. With a set, duplicates are automatically ignored.

unique_visitors = set()
unique_visitors.add("192.168.1.1")
unique_visitors.add("192.168.1.2")
unique_visitors.add("192.168.1.1")  # This won't add a duplicate
print(unique_visitors)  # {'192.168.1.1', '192.168.1.2'}

The magic of sets is speed. Checking if an item exists in a set is nearly instant, even with millions of items. That's because sets use hash tables internally. Lists don't have this luxury.

When to use sets: - Removing duplicates from a list - Checking membership quickly (like "is this username taken?") - Mathematical set operations (union, intersection, difference)

But remember: sets don't maintain order. If you add items in a certain sequence, you can't rely on getting them back in that same order.

Dictionaries: When You Need Labels

Dictionaries are like lists, but instead of numeric indices, you use meaningful keys. Think of it as a real dictionary: you look up a word (the key) and get its definition (the value).

Real example from PythonSkillset: You're building a user profile system. Each user has a name, age, and email. A dictionary makes this natural:

user = {
    "name": "Sarah",
    "age": 28,
    "email": "sarah@pythonskillset.com"
}
print(user["name"])  # "Sarah"

Dictionaries are incredibly fast for lookups by key. Need to find a user by their ID? A dictionary with user IDs as keys will find it instantly, even with millions of users.

When dictionaries shine: - Storing related properties (like a user profile) - Creating lookup tables (like mapping country codes to country names) - Counting occurrences of items (using keys as items and values as counts)

The Performance Reality Check

Let's talk numbers. PythonSkillset ran a simple test: checking if an item exists in a collection of 100,000 elements.

  • List: ~5 milliseconds (scans through every item)
  • Set: ~0.0001 milliseconds (uses hash lookup)
  • Dictionary: ~0.0001 milliseconds (same hash magic)

That's a 50,000x difference. For small collections (under 100 items), you won't notice. But for anything larger, the choice matters.

When to Use Each One

Use a List When:

  • You need to maintain order (like a playlist)
  • You need duplicates (like a shopping cart with multiple quantities of the same item)
  • You need to access items by position (first, last, third)
  • You're building a stack or queue

Use a Set When:

  • You need to ensure uniqueness (like tracking which users have completed a task)
  • You're doing mathematical set operations (like finding common items between two groups)
  • You need lightning-fast membership checks
  • Order doesn't matter

Use a Dictionary When:

  • You need to associate values with keys (like a phonebook)
  • You need fast lookups by a meaningful identifier
  • You're counting occurrences of items
  • You need to store structured data with named fields

The Tricky Part: When They Overlap

Sometimes you can use either a list or a set. For example, removing duplicates from a list:

# Using a set (fast but loses order)
items = [3, 1, 2, 1, 3]
unique_items = list(set(items))  # [1, 2, 3] - order not guaranteed

# Using a loop (preserves order)
seen = set()
ordered_unique = []
for item in items:
    if item not in seen:
        ordered_unique.append(item)
        seen.add(item)
# ordered_unique = [3, 1, 2]

The second approach is slower but preserves the original order. This is a common trade-off you'll face.

A Practical Decision Flow

When I'm coding at PythonSkillset, I ask myself three questions:

  1. Do I need to look things up by a key? → Use a dictionary
  2. Do I need to check if something exists quickly, and duplicates are bad? → Use a set
  3. Do I need order, duplicates, or index access? → Use a list

That's it. Most of the time, the answer is obvious once you think about what you're actually doing with the data.

The One That Surprises Beginners

Here's something that trips up a lot of new Python developers: you can use a dictionary to count things, and it's often better than a list.

# Counting words in a sentence
sentence = "the cat sat on the mat"
word_counts = {}
for word in sentence.split():
    if word in word_counts:
        word_counts[word] += 1
    else:
        word_counts[word] = 1
# {'the': 2, 'cat': 1, 'sat': 1, 'on': 1, 'mat': 1}

Or even cleaner with defaultdict or Counter from collections, but that's a topic for another day.

The Memory Trade-Off

Sets and dictionaries use more memory than lists because of their hash table overhead. For small collections (under 100 items), the difference is negligible. For millions of items, it can add up.

Rule of thumb: If you're storing 10,000+ items and only need to iterate through them, use a list. If you need fast lookups, the extra memory is worth it.

What About Tuples?

I know I said three data types, but tuples deserve a quick mention. Tuples are like immutable lists. Use them when you have a fixed collection that shouldn't change, like coordinates or configuration settings.

# Good use of a tuple
coordinates = (40.7128, -74.0060)  # New York City

The Common Mistake I See

At PythonSkillset, we often see beginners using lists for everything. Then they write code like this:

# Slow way to check if a username exists
usernames = ["alice", "bob", "charlie"]
if "dave" in usernames:  # This scans the entire list
    print("Found!")

For a list of 100 usernames, this is fine. For 100,000, it's painful. A set would be instant:

usernames = {"alice", "bob", "charlie"}
if "dave" in usernames:  # Instant check
    print("Found!")

The Bottom Line

There's no "best" data structure. There's only the right one for your specific situation. Think about what you're doing with your data:

  • Iterating in order? → List
  • Checking if something exists? → Set
  • Looking up by a key? → Dictionary

Once you internalize this, your code will be faster, cleaner, and easier to understand. And that's what PythonSkillset is all about.

Comments

Questions, corrections, and tips stay visible for everyone reading this page.

0 in thread

Join the discussion

Shown next to your comment.

Up to 4,000 characters

No comments yet

Be the first to leave a note — it helps the next reader.