Problem with list of strings in python
list, python
Solution
As others said, it's by design.
Why is it so ? Mostly for historical reasons : C also does it.
In some cases it's handy because it reduce syntaxic noise and avoid adding unwanted spaces (inline SQL queries, complexes regexpes, etc).
What you can do about it ? Not much, but if it really happens often for you, try one of the following tricks.
- indent your list with coma at the beginning of the line. It's weird, but if you do so the missing comas become obvious.
- assign strings to variables and use variables list whenever you can (and it's often a good idea for other reasons, like avoiding duplicate strings).
split your list: for list of words you can put the whole list inside only one string and split it like below. For more than 5 elements it's also shorter.
'a b c d e'.split(' ').
Problem
Why on Earth doesn't the interpreter raise SyntaxError everytime I do this: ``` my_abc = ['a', 'b', 'c' 'd',] ``` I just wanted to add 'c' to the list of strings, and forgot to append the comma. I would expect this to cause some kind of error, as it's cleary incorrect. Instead, what I got: ``` >>> my_abc ['a', 'b', 'cd'] ``` And this is never what I want. Why is it automatically concatenated? I can hardly count how many times I got bitten by this behavior. Is there anything I can do with it? Just to clarify*: I don't actually mind auto-concatenation, my problem has to do ONLY with lists of strings, because they often do much more than just carry text, they're used to control flow, to pass field names and many other things.