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?
str.join()works on any iterable, incl.str, so there will be noTypeErrorstr(L), you iterate overLitself.x = ','.join(str(L))explicitly usingFirst,Secondwhen listing the problems. The OP receivingTypeErrorwithx = ','.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...