# **Python Data Structures (Brief Overview)**
### **1. Lists (`[]`)**
- **Mutable**, ordered collection
- Can store **different data types**
- **Dynamic** (can grow/shrink)
- **Methods:** `append()`, `remove()`, `sort()`, etc.
- **Use Case:** When you need a modifiable sequence.
```python
my_list = [1, "hello", 3.14, True]
my_list.append(5) # [1, "hello", 3.14, True, 5]
```
---
### **2. Tuples (`()`)**
- **Immutable**, ordered collection
- **Faster than lists** (fixed size)
- **Use Case:** Fixed data (e.g., coordinates, dictionary keys).
```python
my_tuple = (1, 2, 3)
print(my_tuple[0]) # 1
```
---
### **3. Dictionaries (`{}`)**
- **Key-value pairs** (unordered in Python <3.7, ordered in ≥3.7)
- **Keys must be unique & immutable** (e.g., `str`, `int`, `tuple`)
- **Methods:** `keys()`, `values()`, `get()`, `update()`
- **Use Case:** Fast lookups (like a real-world dictionary).
```python
my_dict = {"name": "Alice", "age": 25}
print(my_dict["name"]) # "Alice"
```
---
### **4. Sets (`{}`)**
- **Unordered**, **unique elements** (no duplicates)
- **Mutable**, but elements must be **hashable** (immutable)
- **Methods:** `add()`, `remove()`, `union()`, `intersection()`
- **Use Case:** Removing duplicates, membership tests.
```python
my_set = {1, 2, 3, 3} # {1, 2, 3}
my_set.add(4)
```
---
### **5. Arrays (`array.array`)**
- **Homogeneous** (all elements same type)
- More **memory-efficient** than lists for large data
- **Requires `import array`**
- **Use Case:** Numeric data when NumPy is overkill.
```python
import array
int_array = array.array('i', [1, 2, 3])
```
---
### **6. Strings (`""` or `''`)**
- **Immutable** sequence of characters
- **Support indexing/slicing**
- **Methods:** `split()`, `join()`, `upper()`, etc.
- **Use Case:** Text processing.
```python
text = "Hello, World!"
print(text[0]) # 'H'
```
---
### **Comparison Table**
| **Structure** | **Mutable?** | **Ordered?** | **Duplicates?** | **Use Case** |
✅ ✅ ✅
|--------------|-------------|-------------|----------------|-------------|
❌ ✅ ✅
| **List** | Yes | Yes | Allowed | Dynamic collections |
✅ ✅ ❌
| **Tuple** | No | Yes | Allowed | Fixed data |
✅ ❌ ❌
| **Dictionary** | Yes | (≥Python 3.7) | Keys must be unique | Key-value storage |
| **Set** | Yes | No | No | Unique elements |
✅ Yes | ✅ Yes | ✅ Allowed | Numeric data |
❌ No | ✅ Yes | ✅ Allowed | Text |
| **Array** |
| **String** |
---
### **When to Use Which?**
- **Need modification?** → **List**
- **Unchangeable data?** → **Tuple**
- **Fast key-based access?** → **Dictionary**
- **Unique elements?** → **Set**
- **Numbers only?** → **Array** (or **NumPy** for advanced math)
- **Working with text?** → **String**
Would you like examples for any specific use case? 😊