'comma after every element in list instead of the last one
I want to make a function which would add comma after every word in list instead of the last one. I tried to do it with a while-loop so it could stop before the comma reaches the last word, but the output of the code is not doing its job.
list = ['Sadzo', 'Nimadaro', 'Duol']
wordIndex = 0
LenghtUnderIndex = len(list[wordIndex])
while wordIndex <= len(list):
list.insert(LenghtUnderIndex, ', ')
wordIndex += 1
break
print (*list)
Solution 1:[1]
This is what join is for.
out = ','.join(list)
However, do not name your variable list. That hides the Python type by the same name.
Solution 2:[2]
Why not just use:
for x in range(len(list) - 1):
list[x] = list[x] + ","
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tim Roberts |
| Solution 2 | Perry45 |
