Home > Software design >  why there is no error for a= [10,] but error for [10,,] in python?
why there is no error for a= [10,] but error for [10,,] in python?

Time:11-05

why is there no error for a = [10,] but error for [10,,] in Python ? enter image description here
what is interpreter expecting from comma/s in both the cases?

I understand it's more of a syntax error but there has to rationale/logical explanation of this.

CodePudding user response:

Of course there is a logical explanation.

Many people, when writing a list, leave in the extra final comma, as in

lst = [1,2,3,4,]

So, Python allows that and ignores a final comma. But you can't have an empty entry in the MIDDLE of a list.

CodePudding user response:

In python list we cannot have an empty entry in middle of two values. We need to put either any value of any datatype or None.

-->In this the pyhton will ignore the last comma as no value is after that.

lst = [10,20,30,]

--> It shows there is an empty entry before the end of list and its not permissible. Thats why it will throw an error.

lst = [10,,]
  • Related