3

So I'm wondering what the most "pythonic" way of turning a list into a string.

For example:

string_list = ['h', 'e', 'l', 'l', 'o']
# to the string
string = 'hello'

I've been using the ''.join(string_list) method but it just feels near unreadable and roundabout method to do something so simple.

Is there a better way or am I overcomplicating this?

5
  • 4
    Believe it or not, that is the readable way but it's also a bit of a python thing. Commented Aug 5, 2012 at 13:07
  • 4
    ''.join(list) is a very pythonic way already, are you looking for a better efficiency? Commented Aug 5, 2012 at 13:08
  • 1
    On another note, don't call your list list, you are shadowing the builtin. Commented Aug 5, 2012 at 13:09
  • Yeah not an efficiency thing, it just feels ugly to me. But if its the usual way! Commented Aug 5, 2012 at 13:18
  • @InbarRose It is the most efficient way Commented Aug 5, 2012 at 13:23

3 Answers 3

5

No, that is the pythonic way to do it.

Maybe you feel that it should read: string_list.join(''). You are not alone.

Probably the most important advantage is, that this enables .join() to work with anything that is iterable.

If it worked the other way around, each collection would need to implement a join() method by themselves. If you were to create your own collection, would you add a .join() method? Probably not.

The fact that it is a method of the str class means, that it will always work. There are no surprises. Read here on Python and the principle of least astonishment about join() and other things by the Flask author Armin Ronacher.

A similar argument can be made for the len() function/operator, which can be found at the beginning of the aforementioned article.

Sign up to request clarification or add additional context in comments.

1 Comment

Off-topic comment alert. From the (excellent) linked article: "Decorators are somewhat of a pain because there is a difference between @foo and @foo(). If they were declared in a way that the former means the latter we would all be much happier now. Every time I want to introduce a parameter to a previously parameterless decorator makes me want to hit myself and the author of the decorator PEP with a stick." LOL. Indeed!
4

Use ''.join(string_list). Note that list names a built-in type, so you should not use that for variable names. Since Python strings are immutable, you need to construct a new string regardless. ''.join(s) is the canonical and efficient way to do this in Python.

8 Comments

isnt there a more efficient way? since i always thought ''.join(s) is like writing s[0]+''+s[1]+''+s[2]+''+s[3]...s[x] ?
@InbarRose the + method creates n temporary copies. The str.join method reserves required space in a single pre-pass. So the .join method will outperform the naive + operator for non-trivial cases, especially those cases where there are many strings to join
The .join() string method is actually the most efficient method of assembling a multipart string in Python. String addition is slow because it creates many strings: first it creates s[0]+'', then to that resulting string it adds s[1] to create another. .join() only creates a single string.
@InbarRose regardless of how the "gap" is filled, n new strings still need to be created, with all but the last being temporary, due to the simple way the + operator overload is expanded and handled in the AST
@InbarRose Also Py_MEMCPY(p, sep, seplen); will do a memcpy on a seplen of 0 in the case of ''.join, so nothing will actually end up being copied.
|
2

I think you're overcomplicating. Here is from python itself:

>>> list = ['h', 'e', 'l', 'l', 'o']
>>> sum(list, '')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sum() can't sum strings [use ''.join(seq) instead]

So, python's recommendation is to use ''.join.

3 Comments

this is not what the OP asked. he wants to convert a list of strings into a single string which is made up of all the strings in the list.
@InbarRose This was the old way of doing that
Hm, I just delivered python answer on given question (converting list of string into string).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.