0

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.

S.B
  • 13,077
  • 10
  • 22
  • 49
Arpit Sisodia
  • 570
  • 5
  • 18
  • 2
    It's just a syntax error... I don't know what you are expecting as an explanation. – j1-lee Nov 05 '22 at 03:52
  • 2
    Does this answer your question? [Why are trailing commas allowed in a list?](https://stackoverflow.com/questions/11597901/why-are-trailing-commas-allowed-in-a-list) – S.B Nov 05 '22 at 06:46

2 Answers2

2

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.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • +1. Especially when they write their list vertically. *Black* also formats long lists vertically with a trailing comma. – S.B Nov 05 '22 at 06:44
0

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,,]
Luicfer Ai
  • 56
  • 2