# 8 mistakes almost every beginner makes with lists.
# Wrong way, error, right way — all in one file.
# redhorndev.com

# ─────────────────────────────────────────────
# 1. Capturing None from a mutating method
# ─────────────────────────────────────────────

squad = ["Raven", "Wolf"]
squad = squad.append("Ghost")   # WRONG — append returns None
print(squad)                    # None

squad = ["Raven", "Wolf"]
squad.append("Ghost")           # CORRECT — no assignment
print(squad)                    # ['Raven', 'Wolf', 'Ghost']

# same applies to: sort(), reverse(), clear(), insert(), extend(), remove()

# ─────────────────────────────────────────────
# 2. IndexError — going out of bounds
# ─────────────────────────────────────────────

squad = ["Raven", "Wolf", "Ghost"]
# print(squad[3])               # IndexError: list index out of range
                                # 3 elements → valid indexes: 0, 1, 2

print(squad[2])                 # CORRECT — last element
print(squad[-1])                # CORRECT — last element, any length

# ─────────────────────────────────────────────
# 3. remove() on a value that isn't there
# ─────────────────────────────────────────────

squad = ["Raven", "Wolf", "Ghost"]
# squad.remove("Fox")           # ValueError: list.remove(x): x not in list

if "Fox" in squad:              # CORRECT — check first
    squad.remove("Fox")
else:
    print("Fox not in squad.")  # Fox not in squad.

# ─────────────────────────────────────────────
# 4. Confusing append() and extend()
# ─────────────────────────────────────────────

squad = ["Raven", "Wolf"]
squad.append(["Ghost", "Viper"])
print(squad)                    # ['Raven', 'Wolf', ['Ghost', 'Viper']]
                                # nested list — probably not what you wanted

squad = ["Raven", "Wolf"]
squad.extend(["Ghost", "Viper"])
print(squad)                    # ['Raven', 'Wolf', 'Ghost', 'Viper']
                                # CORRECT — elements added individually

# ─────────────────────────────────────────────
# 5. Modifying a list while looping over it
# ─────────────────────────────────────────────

numbers = [1, 2, 3, 4, 5]
for num in numbers:
    if num % 2 == 0:
        numbers.remove(num)     # WRONG — shifts elements, skips some
print(numbers)                  # [1, 3, 5] — looks right, but unreliable

numbers = [1, 2, 3, 4, 5]
odd = []
for num in numbers:             # CORRECT — build a new list instead
    if num % 2 != 0:
        odd.append(num)
print(odd)                      # [1, 3, 5]

# ─────────────────────────────────────────────
# 6. Assignment is not a copy
# ─────────────────────────────────────────────

original = ["Raven", "Wolf", "Ghost"]
backup = original               # WRONG — same list, two names
backup.append("Viper")
print(original)                 # ['Raven', 'Wolf', 'Ghost', 'Viper']
                                # original changed too

original = ["Raven", "Wolf", "Ghost"]
backup = original.copy()        # CORRECT — independent copy
backup.append("Viper")
print(original)                 # ['Raven', 'Wolf', 'Ghost']
print(backup)                   # ['Raven', 'Wolf', 'Ghost', 'Viper']

# ─────────────────────────────────────────────
# 7. Using index() without checking first
# ─────────────────────────────────────────────

squad = ["Raven", "Wolf", "Ghost"]
# print(squad.index("Fox"))     # ValueError: 'Fox' is not in list

if "Fox" in squad:              # CORRECT — check before calling index()
    print(squad.index("Fox"))
else:
    print("Not found.")         # Not found.

# ─────────────────────────────────────────────
# 8. Forgetting that slice stop is exclusive
# ─────────────────────────────────────────────

squad = ["Raven", "Wolf", "Ghost", "Viper", "Bull"]

print(squad[0:3])               # ['Raven', 'Wolf', 'Ghost'] — indexes 0, 1, 2
                                # index 3 is NOT included

print(squad[:3])                # same — first 3 elements
print(squad[:4])                # ['Raven', 'Wolf', 'Ghost', 'Viper'] — up to and including index 3

# ─────────────────────────────────────────────
# Quick reference
# ─────────────────────────────────────────────

# append() / sort() / reverse()  → return None — don't assign
# squad[3] on a 3-element list   → IndexError
# squad.remove("x")              → ValueError if "x" not in list — check with in first
# append([a, b])                 → adds a nested list — use extend() instead
# modify list during loop        → unpredictable — build a new list instead
# backup = original              → same list — use .copy() for independence
# squad.index("x")               → ValueError if missing — check with in first
# list[0:3]                      → indexes 0, 1, 2 — stop is exclusive
