13

I'm new to python and this is just to automate something on my PC. I want to concatenate all the items in a list. The problem is that

''.join(list)

won't work as it isn't a list of strings.

This site http://www.skymind.com/~ocrow/python_string/ says the most efficient way to do it is

''.join([`num` for num in xrange(loop_count)])

but that isn't valid python...

Can someone explain the correct syntax for including this sort of loop in a string.join()?

1

4 Answers 4

19

You need to turn everything in the list into strings, using the str() constructor:

''.join(str(elem) for elem in lst)

Note that it's generally not a good idea to use list for a variable name, it'll shadow the built-in list constructor.

I've used a generator expression there to apply the str() constructor on each and every element in the list. An alternative method is to use the map() function:

''.join(map(str, lst))

The backticks in your example are another spelling of calling repr() on a value, which is subtly different from str(); you probably want the latter. Because it violates the Python principle of "There should be one-- and preferably only one --obvious way to do it.", the backticks syntax has been removed from Python 3.

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

1 Comment

Ah. Thanks. All these differences between v2 and v3 are really annoying, and I can't yet tell which version a googled piece of code will work in!
6

Here is another way (discussion is about Python 2.x):

''.join(map(str, my_list))

This solution will have the fastest performance and it looks nice and simple imo. Using a generator won't be more efficient. In fact this will be more efficient, as ''.join has to allocate the exact amount of memory for the string based on the length of the elements so it will need to consume the whole generator before creating the string anyway.

Note that `` has been removed in Python 3 and it's not good practice to use it anymore, be more explicit by using str() if you have to eg. str(num).

1 Comment

@gnibbler noted that at the top
2

just use this, no need of [] and use str(num):

''.join(str(num) for num in xrange(loop_count))  

for list just replace xrange(loop_count) with the list name.

example:

   >>> ''.join(str(num) for num in xrange(10)) #use range() in python 3.x
    '0123456789'

1 Comment

Not really what the OP was asking for; the for num in xrange(loop_count) example is a distraction, really.
2

If your Python is too old for "list comprehensions" (the odd [x for x in ...] syntax), use map():

 ''.join(map(str, list))

6 Comments

The fact that the OP got a syntax error is an indication we are talking about Python 3.x here; it'll be new enough for a generator expression. :-)
@MartijnPieters: I read that as the exact opposite: He has a syntax error and doesn't know how to fix it.
The code is legal in Python 2; I was addressing your `python is too old for list comprehensions remark. :-)
He might be using Python 1.6.
Highly unlikely, that hasn't been current for over a decade!
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.