1

I am trying to concatenate items in a list onto a string.

list = ['a', 'b', 'c', 'd']
string = ''
for i in list:
    string.join(str(i))
0

2 Answers 2

6

You don't need a loop:

items = ['a', 'b', 'c', 'd']
result = "".join(items)

Note that it's a bad idea to use list as the name of a variable, because that prevents you from using list to mean the built-in type.

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

Comments

6

Is this what you are looking for?

>>> my_list = ['a', 'b', 'c', 'd']
>>> "".join(my_list)
'abcd'

You shouldn't use list as a variable name, since this will shadow the built-in class list.

2 Comments

Wow, you and I posted essentially the exact same answer within 10 seconds of each other. +1
@DanielPryden: This happened to me even within the same second. It is a bit more surprising for this post, though, since the "question" isn't the clearest one ever posted on SO. (+1 for you.)

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.