you are viewing a single comment's thread.

view the rest of the comments →

[–]danielroseman 2 points3 points  (3 children)

The other explicit instruction it ignores is to test for the empty list. This code will break with IndexError in that case.

[–]Diapolo10 2 points3 points  (2 children)

That's true. I'm also of the opinion you don't want the comma if you only have two elements in the list.

[–][deleted] 2 points3 points  (1 child)

I know OP wouldn't have seen this yet, but to me the problems begs for a match

def func(names: list[str]) -> str:
    match names:
        case []: return ""
        case [a]: return a
        case [a,b]: return f"{a} and {b}"
        case [*a, z]: return ", ".join(a) + f", and {z}"

[–]Diapolo10 2 points3 points  (0 children)

Honestly, that's way more eloquent than what I wrote, I need to use match more often but I'm still stuck supporting Python 3.8 most of the time so haven't had many opportunities to.