Python Data Structures - Quick Guide
Python Data Structures - Quick Guide
## **1. Lists**
- Mutable, ordered sequences
- Mixed data types allowed
- Example: `colors = ["red", "blue", "green"]`
## **2. Tuples**
- Immutable, ordered sequences
- Faster than lists
- Example: `point = (3, 5)`
## **3. Dictionaries**
- Key-value pairs
- Fast lookups
- Example: `user = {"name": "Sam", "age": 30}`
## **4. Sets**
- Unique elements only
- Unordered
- Example: `unique_nums = {1, 2, 3}`
## **5. Arrays**
- Homogeneous data
- Memory efficient
- Example: `import array; nums = array.array('i', [1, 2, 3])`
## **6. Strings**
- Immutable text
- Example: `msg = "Hello"`
**When to Use:**
- Change data? → **List**
- Fixed data? → **Tuple**
- Fast lookups? → **Dictionary**
- Unique values? → **Set**
- Numbers only? → **Array**
- Text? → **String**