Hacker News new | ask | show | jobs
by BoppreH 1066 days ago
Pretty good list. Two corrections:

The `first, *middle, last` trick doesn't work if your list only has one element:

  first, *middle, last = [1]
  ValueError: not enough values to unpack (expected at least 2, got 1)
And the last title has a typo:

> Separater for Large Numbers

1 comments

I don't think the point about splat unpacking really is a correction. Unpacking always requires that the iterable has enough values to assign to the specified variables, this has nothing to do with the use of *middle.
To be clear, this is only a problem if your array can ever have less than two values, or if you require that `first` and `last` be different values.

The mistake is suggesting the unpacking without caveats, because it'll fail in situations that the naive solution doesn't:

    items = [1]
    first = items[0]
    middle = items[1:-1]
    last = items[-1]
This will still work as long as you have any elements.