Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is it string.join(list) instead of list.join(string)?

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?

like image 486
Evan Fosmark Avatar asked Jan 29 '09 22:01

Evan Fosmark


People also ask

Why is join a string method?

Python String join() Method The join() method takes all items in an iterable and joins them into one string. A string must be specified as the separator.

How do I join a list into a string?

To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.

How do you join a list of strings in Python?

If you want to concatenate a list of numbers ( int or float ) into a single string, apply the str() function to each element in the list comprehension to convert numbers to strings, then concatenate them with join() .

What is string join in Python?

Python String join() The join() string method returns a string by joining all the elements of an iterable (list, string, tuple), separated by a string separator.


2 Answers

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

like image 183
recursive Avatar answered Sep 21 '22 18:09

recursive


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:
    • str.join(seq)
    • seq.join(str)
    • seq.reduce(str)
    • join as a built-in function
  • Guido wanted to support not only lists and tuples, but all sequences/iterables.
  • seq.reduce(str) is difficult for newcomers.
  • seq.join(str) introduces unexpected dependency from sequences to str/unicode.
  • join() as a built-in function would support only specific data types. So using a built-in namespace is not good. If join() supports many datatypes, creating an optimized implementation would be difficult, if implemented using the __add__ method then it would ve O(n²).
  • The separator string (sep) 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 UCS2/4. To calculate total buffer length of UTF-8 strings it needs to know character coding rule.
  • 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 iterable class (which is mentioned in another comment).

Guido's decision is recorded in a historical mail, deciding on str.join(seq):

Funny, but it does seem right! Barry, go for it...
Guido van Rossum

like image 30
Yoshiki Shibukawa Avatar answered Sep 22 '22 18:09

Yoshiki Shibukawa