Dictionaries and Sets

Learn about Python's key-value stores and unique collections

35 minutesBeginner

Learning Objectives

  • Understand dictionary structure and usage
  • Master dictionary operations and methods
  • Learn about sets and their operations
  • Practice with real-world examples

Dictionaries and Sets in Python

Introduction to Dictionaries

A dictionary is a collection of key-value pairs, where each key must be unique. Think of it like a real dictionary where each word (key) has its definition (value).

Key characteristics of dictionaries:

  • Keys must be unique and immutable
  • Values can be of any type
  • Unordered (in Python < 3.7)
  • Mutable (can be modified)

Key Points:

  • Dictionaries store key-value pairs
  • Keys must be unique
  • Values can be any type
  • Dictionaries are mutable
Initializing Python environment...

Dictionary Operations

Accessing and Modifying

Dictionaries provide several ways to access and modify their contents:

  • Using square brackets: dict[key]
  • Using get() method: dict.get(key, default)
  • Adding/updating items: dict[key] = value
  • Removing items: del dict[key]

Key Points:

  • Multiple ways to access values
  • Safe access with get()
  • Easy to modify and update
  • Remove items with del

Dictionary Methods

Current Dictionary

{ "name": "Bob", "age": 25, "city": "New York" }

History

my_dict = {"name": "Bob", "age": 25, "city": "New York"}
Initializing Python environment...
Initializing Python environment...

Introduction to Sets

A set is an unordered collection of unique elements. Sets are perfect for:

  • Removing duplicates from sequences
  • Testing membership
  • Mathematical set operations

Key characteristics:

  • Elements must be immutable
  • No duplicate elements
  • Unordered collection
  • Mutable (can add/remove elements)

Key Points:

  • Sets store unique elements
  • Elements must be immutable
  • Unordered collection
  • Perfect for membership testing
Initializing Python environment...

Set Operations

Set A

1
2
3
4

Set B

3
4
5
6

Result of union

1
2
3
4
5
6
# Python Set Operation
set1 = ["1","2","3","4"]
set2 = ["3","4","5","6"]
result = ["1","2","3","4","5","6"]

Performance Considerations

Dictionary Performance

  • Hash table implementation provides O(1) average case for lookups
  • Memory overhead for hash table structure
  • Performance impact of hash collisions
  • Resizing operations when dictionary grows

Set Performance

  • Also uses hash table implementation
  • O(1) average case for membership testing
  • Memory-efficient for storing unique values
  • Set operations are highly optimized

Best Practices for Performance

  • Choose appropriate initial size when possible
  • Use dict.get() instead of try-except for expected missing keys
  • Use set operations instead of loops for membership testing
  • Consider frozenset for immutable set requirements

Key Points:

  • Understand time complexity
  • Consider memory usage
  • Use optimized operations
  • Choose appropriate data structures
Initializing Python environment...

Dictionaries and Sets Quiz

Key Takeaways

  • Understanding of key-value data structures
  • Ability to work with dictionaries effectively
  • Knowledge of set operations and use cases
  • Skills in choosing appropriate data structures
  • Practice with common dictionary and set patterns