It's because any iterable can be joined (e.g, list, tuple, dict, set), but its contents and the "joiner" must be strings.
For example:
'_'.join(['welcome', 'to', 'stack', 'overflow'])
'_'.join(('welcome', 'to', 'stack', 'overflow'))
'welcome_to_stack_overflow'
Using something other than strings will raise the following error:
Answer from recursive on Stack OverflowTypeError: sequence item 0: expected str instance, int found
It's because any iterable can be joined (e.g, list, tuple, dict, set), but its contents and the "joiner" must be strings.
For example:
'_'.join(['welcome', 'to', 'stack', 'overflow'])
'_'.join(('welcome', 'to', 'stack', 'overflow'))
'welcome_to_stack_overflow'
Using something other than strings will raise the following error:
TypeError: sequence item 0: expected str instance, int found
This was discussed in the String methods... finally thread in the Python-Dev achive, and was accepted by Guido. This thread began in Jun 1999, and str.join was included in Python 1.6 which was released in Sep 2000 (and supported Unicode). Python 2.0 (supported str methods including join) was released in Oct 2000.
- There were four options proposed in this thread:
separator.join(items)items.join(separator)items.reduce(separator)joinas a built-in function
- Guido wanted to support not only
lists andtuples, but all sequences/iterables. items.reduce(separator)is difficult for newcomers.items.join(separator)introduces unexpected dependency from sequences to str/unicode.join()as a free-standing built-in function would support only specific data types. So using a built-in namespace is not good. Ifjoin()were to support many data types, creating an optimized implementation would be difficult: if implemented using the__add__method then it would be O(n²).- The separator string (
separator) should not be omitted. Explicit is better than implicit.
Here are some additional thoughts (my own, and my friend's):
- Unicode support was coming, but it was not final. At that time UTF-8 was the most likely about to replace UCS-2/-4. To calculate total buffer length for UTF-8 strings, the method needs to know the character encoding.
- At that time, Python had already decided on a common sequence interface rule where a user could create a sequence-like (iterable) class. But Python didn't support extending built-in types until 2.2. At that time it was difficult to provide basic
iterableclass (which is mentioned in another comment).
Guido's decision is recorded in a historical mail, deciding on separator.join(items):
Funny, but it does seem right! Barry, go for it...
--Guido van Rossum
How would I turn the following code into a function? The goal is to take a list of colors and return a string with "+" in between each color from the list. The code below returns: the best colors are : blue + yellow.
I'm looking to create a function colors(): in which I could pass any list of colors instead of hardcoding it below. For example, colors(['green', 'red', 'black']) would return: the best colors are : green + red + black
input_list = ['blue', 'yellow']
delim = " + "
res = delim.join([str(ele) for ele in input_list])
print('the best colors are' + ' : ' + str(res))