0

ALL,

I have a following list:

["abc", "def", 'ghi"]

What I need to get from it is a string:

"abc,def,ghi"

There is a comma between first and second element, but not at the end of the string.

Unfortunately, Python does not have a construction like:

std::string str;
for( int i = 0; i < 3; i++ )
{
    str += list[i];
    if( i != 2 )
        str += ",";
}

How do I do it in Python?

1 Answer 1

7

Think of it in terms of a goal, not the way you'd do it in C++. You want to join the elements together:

','.join(elements)
Sign up to request clarification or add additional context in comments.

Comments

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.