A Claude Code skill that optimizes Python code generation by selecting the most efficient data structures and number handling strategies based on real-world performance and memory characteristics.
This skill teaches Claude Code to write more efficient Python code by understanding:
- Memory characteristics of Python numbers and collections
- Performance implications of different data structures
- When to use sets vs lists vs dicts
- Memory optimization techniques like
__slots__
This skill implements best practices from the article "Python Numbers Every Programmer Should Know" by Michael Kennedy.
- Sets/Dicts for membership testing: 200x faster than lists for
x in containerchecks - Lists for ordered sequences: Most memory-efficient for iteration
- Proper usage based on access patterns
- Use
__slots__for classes with many instances (30% memory reduction) - Understanding memory footprint: Lists (36KB) < Sets (59.6KB) < Dicts (90.7KB) per 1000 integers
- O(1) lookups with sets/dicts instead of O(n) with lists
- Efficient counting with
Counteror dict - Fast unique value extraction with sets
To use this skill with Claude Code:
-
Copy
python-number-optimization.skillto your Claude Code skills directory:- Linux/macOS:
~/.claude/skills/ - Windows:
%USERPROFILE%\.claude\skills\
- Linux/macOS:
-
Or reference it in your project-specific
.claude/skills/directory
Once installed, Claude Code will automatically apply these optimizations when:
- You ask for "optimized" or "efficient" Python code
- Working with collections and data structures
- The code involves membership testing or lookups
- Creating classes with many instances
You can also explicitly invoke it:
Use the python-optimization skill to write a function that checks if user IDs are valid
See examples/ directory for before/after code samples demonstrating:
- Membership testing optimization
- Counting and grouping patterns
- Class memory optimization with
__slots__ - Container selection for different use cases
| Operation | Best Container | Complexity |
|---|---|---|
Membership test (x in y) |
Set/Dict | O(1) |
| Ordered iteration | List | O(n) |
| Key-value lookup | Dict | O(1) |
| Memory-efficient storage | List | 36KB/1000 ints |
| Unique values | Set | O(n) dedup |
To improve this skill:
- Test it with various Python coding scenarios
- Submit improvements based on real-world usage
- Add more patterns and anti-patterns
MIT