1

So i was studying some example of join method in python and faced an issue .following use of join seems clear

L = ['red', 'green', 'blue']
x = ','.join(L)
print(x)

which produce :

red,green,blue

but using same logic on list of ints cause strange issue

L = [1, 2, 3, 4, 5, 6]
x = ','.join(str(L))
print(x)

which produce :

[,1,,, ,2,,, ,3,,, ,4,,, ,5,,, ,6,]

this can be corrected using for loop as follow

L = [1, 2, 3, 4, 5, 6]
x = ','.join(str(val) for val in L)
print(x)
# Prints 1,2,3,4,5,6

so my question is why list of string does not need for loop to provide correct values while list of ints needs extra for loop to produce correct results?

3
  • 2
    @AbraxasvonAbrasax, str.join() works on any iterable, incl. str, so there will be no TypeError Commented Jul 4, 2022 at 8:56
  • 1
    @AbraxasvonAbrasax So, you are NOT iterating over str(L), you iterate over L itself. Commented Jul 4, 2022 at 8:58
  • @AbraxasvonAbrasax, in your comment you referred to x = ','.join(str(L)) explicitly using First, Second when listing the problems. The OP receiving TypeError with x = ','.join(L) is exactly why they post their question - i.e. no need to tell them that - they know it and ask WHY that happens. And no, there are no redundant brackets... Commented Jul 4, 2022 at 9:02

2 Answers 2

2

It is because str(L) produces string '[1, 2, 3, 4, 5, 6]' and join will then join every single character of that string. The correct and clear method to do what you would like to is either yours:

>>> ','.join(str(i) for i in L)
'1,2,3,4,5,6'

or

>>> ','.join(map(str, L))
'1,2,3,4,5,6'

There are other methods also, but for join to be used you need to get list of strings ['1', '2', '3', '4', '5', '6'] instead of single string '[1, 2, 3, 4, 5, 6]'.

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

3 Comments

strange my python version returns [1, 2, 3, 4, 5, 6] and not '[1, 2, 3, 4, 5, 6]' with quotation .using python 3.6 .
@Amir just check with type(str(L))
@Amir, that can't be true. You probably printed the result. The quotes that "hold" a string are never printed, that's why you probably didn't see them.
0

When you run x = ','.join(str(val) for val in L) you are traversing the list and converting each value to string. Meanwhile, if you only use str()to convert a list to string, you won't be traversing the list, and the output will be wrong.

L = [1, 2, 3, 4, 5, 6]
print(str(L))

[1, 2, 3, 4, 5, 6]

As you can see in the following example, each value of the list is a string value. On the above code the entire list is a string:

L = [1, 2, 3, 4, 5, 6]

print([str(i) for i in L])

['1', '2', '3', '4', '5', '6']

4 Comments

Why does it traverse list when it is all string and it does not while its int ?
@amir Because join only works with strings, not int values. Please take a look at the official Python documentation.
@amir More specifically, the [i for i in thing] syntax is what iterates over (or traverses) the list. It's called a list comprehension.
@amir str(L) turns the whole list into one string; [str(i) for i in L] gives you a new list, consisting of each of the original list's items converted into strings individually.

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.