|
| 1 | +# Lists vs. Sets |
| 2 | + |
| 3 | +## Lists |
| 4 | + |
| 5 | +- **Ordered Collection:** |
| 6 | + - Lists are ordered collections of elements. The order in which elements are added is preserved. |
| 7 | + - Elements can be accessed by their index. |
| 8 | + |
| 9 | + ```python |
| 10 | + my_list = [1, 2, 3, 4, 5] |
| 11 | + print(my_list[0]) # Output: 1 |
| 12 | + ``` |
| 13 | + |
| 14 | +- **Mutable:** |
| 15 | + - Lists are mutable, meaning you can modify their elements after creation. |
| 16 | + |
| 17 | + ```python |
| 18 | + my_list[1] = 10 |
| 19 | + ``` |
| 20 | + |
| 21 | +- **Allows Duplicate Elements:** |
| 22 | + - Lists can contain duplicate elements. |
| 23 | + |
| 24 | + ```python |
| 25 | + my_list = [1, 2, 2, 3, 4] |
| 26 | + ``` |
| 27 | + |
| 28 | +- **Use Cases:** |
| 29 | + - Use lists when you need an ordered collection with the ability to modify elements. |
| 30 | + |
| 31 | +## Sets |
| 32 | + |
| 33 | +- **Unordered Collection:** |
| 34 | + - Sets are unordered collections of unique elements. The order in which elements are added is not preserved. |
| 35 | + - Elements cannot be accessed by their index. |
| 36 | + |
| 37 | + ```python |
| 38 | + my_set = {1, 2, 3, 4, 5} |
| 39 | + ``` |
| 40 | + |
| 41 | +- **Mutable:** |
| 42 | + - Sets are mutable, meaning you can add and remove elements after creation. |
| 43 | + |
| 44 | + ```python |
| 45 | + my_set.add(6) |
| 46 | + ``` |
| 47 | + |
| 48 | +- **No Duplicate Elements:** |
| 49 | + - Sets do not allow duplicate elements. If you try to add a duplicate, it won't raise an error, but the set won't change. |
| 50 | + |
| 51 | + ```python |
| 52 | + my_set = {1, 2, 2, 3, 4} # Results in {1, 2, 3, 4} |
| 53 | + ``` |
| 54 | + |
| 55 | +- **Use Cases:** |
| 56 | + - Use sets when you need an unordered collection of unique elements, and you want to perform set operations like union, intersection, and difference. |
| 57 | + |
| 58 | +### Common Operations: |
| 59 | + |
| 60 | +- **Adding Elements:** |
| 61 | + - Lists use `append()` or `insert()` methods. |
| 62 | + - Sets use `add()` method. |
| 63 | + |
| 64 | +- **Removing Elements:** |
| 65 | + - Lists use `remove()`, `pop()`, or `del` statement. |
| 66 | + - Sets use `remove()` or `discard()` methods. |
| 67 | + |
| 68 | +- **Checking Membership:** |
| 69 | + - Lists use the `in` operator. |
| 70 | + - Sets use the `in` operator as well, which is more efficient for sets. |
| 71 | + |
| 72 | +```python |
| 73 | +# Lists |
| 74 | +if 3 in my_list: |
| 75 | + print("3 is in the list") |
| 76 | + |
| 77 | +# Sets |
| 78 | +if 3 in my_set: |
| 79 | + print("3 is in the set") |
| 80 | +``` |
| 81 | + |
| 82 | +### Choosing Between Lists and Sets |
| 83 | + |
| 84 | +- **Use Lists When:** |
| 85 | + - You need to maintain the order of elements. |
| 86 | + - Duplicate elements are allowed. |
| 87 | + - You need to access elements by index. |
| 88 | + |
| 89 | +- **Use Sets When:** |
| 90 | + - Order doesn't matter. |
| 91 | + - You want to ensure unique elements. |
| 92 | + - You need to perform set operations like union, intersection, or difference. |
0 commit comments