This has always confused me. It seems like this would be nicer:
my_list = ["Hello", "world"]
print(my_list.join("-"))
# Produce: "Hello-world"
Than this:
my_list = ["Hello", "world"]
print("-".join(my_list))
# Produce: "Hello-world"
Is there a specific reason it is like this?
Answer
It's because any iterable can be joined, not just lists, but the result and the "joiner" are always strings.
For example:
import urllib2
print('\n############\n'.join(
urllib2.urlopen('http://data.stackexchange.com/users/7095')))
No comments:
Post a Comment