I can never remember that `joinedList = ",".join(listOfThings)` is how Python joins together a list of things with commas because it is 100% bass-awkwards of how I think of the operation of joining together a list of things, which would be spelled `joinedList = listOfThings.join(",")`

Literally every time I'm away from Python for more than like, a week, I end up searching for how to do this. >_<

@genehack The way I think of it is you’re asking an immutable thing (a string) to join a mutable thing (a list). The other way around would on the surface imply an action on a mutable thing.

Also: you’re asking a like thing to make a like thing (asking a string to make a string). “Hey comma, join this shit together for me would you?”

My complaint is it doesn’t str the elements while doing so. 🤷🏻‍♀️

@davep i mean, that's the thing: (in my head) the action *IS* on the mutable thing, it just happens to be an action that returns a new value instead of mutating the thing.

@genehack I could have worded that better: I meant “imply that a mutable thing is being asked to do this acton”. Contrast with:

>>> l = [3,2,1]
>>> l.sort()
>>> l
[1, 2, 3]

When you ask a list to sort, it mutates itself. It would sort of look odd that you’re asking it to join as it would suggest that it will join itself… somehow?

But…

@genehack …I think the thing that’s even more important is that `str.join` is for joining any sort of iterable. As well as lists there’s also sets and tuples and so on, and indeed anything that implements __iter__. Eg:

```
class Stuff:

def __iter__(self):
return (str(n) for n in range(30))

print(",".join(Stuff()))
```

So Stuff doesn’t need to grow a `join` method.

Follow

@davep @genehack I'm not convinced. Even implode(list, ",") makes more sense. But I'm partial to list.map(v => v.toString()).join(","), just makes more sense to me. And yes, I'm converting a list of objects to a list of strings, to a string.

@pies @genehack I don’t think I understand what you’re not convinced about. This is what join is in Python. I’m not trying to be convincing, I’m just stating a fact of its design and utility (and offering how I remember it so it flows off my fingers).

@davep @genehack I just meant I think it's not very intuitive.

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.