Python Sets vs. Dictionaries: Key Differences for AI

Unlock Python's power: Understand the core differences between sets and dictionaries, crucial for efficient data handling in AI and machine learning projects.

3.12 Difference Between Sets and Dictionaries in Python

Both sets and dictionaries in Python are built upon hash table implementations, leading to similarities in their underlying mechanics and the use of curly braces {} in their syntax. However, they serve distinct purposes and exhibit different behaviors.

Sets

A set is an unordered collection of unique elements. It exclusively stores values and is primarily used for:

  • Membership testing: Quickly checking if an element exists within the collection.
  • Duplicate elimination: Automatically discarding any duplicate values when elements are added.
  • Mathematical set operations: Performing operations like union, intersection, difference, and symmetric difference.

Dictionaries

A dictionary is an unordered collection of key-value pairs, where each key must be unique. Dictionaries are used for:

  • Fast lookups: Retrieving a value associated with a specific key.
  • Mappings and associations: Establishing relationships between different pieces of data.

Feature-wise Comparison: Set vs. Dictionary

Feature / AspectSetDictionary
DefinitionUnordered collection of unique elementsUnordered collection of key-value pairs
SyntaxDefined using {element1, element2, ...} or set()Defined using {key1: value1, key2: value2, ...} or dict()
StructureStores only valuesStores data as key: value pairs
UniquenessAll elements must be uniqueKeys must be unique; values can be duplicated
MutabilityMutable – elements can be added or removedMutable – keys and values can be modified
Access MechanismNo direct index/key access; accessed via iterationAccessed directly using keys
Primary Use CaseRemoving duplicates, membership testing, set operationsAssociative mapping and quick lookups
Order PreservationUnordered (historically). In Python 3.7+, insertion order is preserved.Insertion order preserved (Python 3.7+).

Code Examples

Set Example

# Creating a set
my_set = {1, 2, 3, 2}
print(my_set)
# Output: {1, 2, 3} (duplicates are automatically removed)

# Adding an element
my_set.add(4)
print(my_set)
# Output: {1, 2, 3, 4} (order may vary)

# Membership test
print(2 in my_set)
# Output: True

# Set operations
set_a = {1, 2, 3}
set_b = {3, 4, 5}
print(set_a.union(set_b))      # Union: {1, 2, 3, 4, 5}
print(set_a.intersection(set_b)) # Intersection: {3}
print(set_a.difference(set_b))  # Difference: {1, 2}

Dictionary Example

# Creating a dictionary
my_dict = {'name': 'Alice', 'age': 25}
print(my_dict)
# Output: {'name': 'Alice', 'age': 25}

# Accessing a value
print(my_dict['name'])
# Output: Alice

# Modifying a value
my_dict['age'] = 26
print(my_dict)
# Output: {'name': 'Alice', 'age': 26}

# Adding a new key-value pair
my_dict['city'] = 'New York'
print(my_dict)
# Output: {'name': 'Alice', 'age': 26, 'city': 'New York'}

# Safe key access using .get()
print(my_dict.get('age'))       # Output: 26
print(my_dict.get('salary'))    # Output: None (key doesn't exist, returns None)
print(my_dict.get('salary', 0)) # Output: 0 (key doesn't exist, returns default value)

Key Takeaways

  • Sets are optimized for storing unique values and performing efficient set-theoretic operations. They are your go-to for tasks involving uniqueness, duplicate removal, and membership checks.
  • Dictionaries excel at creating mappings between keys and values, enabling rapid retrieval of information based on its associated key. They are fundamental for representing structured data and associations.

Choosing the right data structure between sets and dictionaries is crucial for writing efficient, readable, and optimized Python code.

Interview Questions

  1. What is the primary difference between a Python set and a dictionary?
  2. How are sets and dictionaries typically implemented in Python?
  3. Can dictionary keys be duplicated? What about set elements?
  4. How do you add and remove elements from a set?
  5. How do you access values in a dictionary?
  6. When would you choose to use a set over a dictionary in a Python program, and vice versa?
  7. How does Python maintain order in sets and dictionaries, especially with newer Python versions?
  8. Explain a practical use case for using a set in Python.
  9. Explain a practical use case for using a dictionary in Python.
  10. What happens if you try to access a key that does not exist in a dictionary? How can you handle this gracefully?