1

Is there any difference between these two way to define list in Python?

lst1=[1,2]
lst2=[1,2,]

If there is no difference between then, Why Python define these two way?

TheGoodUser
  • 1,188
  • 4
  • 26
  • 52

1 Answers1

1

Mostly for convenience, especially when defining multi-line lists.

mylist = [
    1,
    2,
    3,
]

The trailing comma makes it a little easier to add new items or reorder them. If you get in the habit of always leaving a trailing comma, then bugs due to forgetting to remove a comma go away.

Roger Fan
  • 4,945
  • 31
  • 38
  • 3
    It also means that if you add a new item to the list, the diff will only be 1 line, not 2. This sounds like a very minor thing, but when working in large groups it becomes important because it makes "git diff" look a lot cleaner, makes reverts and merges more likely to happen without fail. – TomOnTime Sep 26 '14 at 18:02