It is well known that tuples are not defined by brackets, but by commas. Quote from the documentation :
A tuple consists of several values separated by commas
Thus:
myVar1 = 'a', 'b', 'c'
type(myVar1)
<type 'tuple'>
Another striking example:
myVar2 = ('a')
type(myVar2)
<type 'str'>
myVar3 = ('a',)
type(myVar3)
<type 'tuple'>
Even a singleton tuple needs a comma, and parentheses are always used to avoid confusion. My question is: Why can't we omit the parentheses of arrays in list comprehension? For example:
myList1 = ['a', 'b']
myList2 = ['c', 'd']
print([(v1,v2) for v1 in myList1 for v2 in myList2])
[('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd')]
print([v1,v2 for v1 in myList1 for v2 in myList2])
SyntaxError: invalid syntax
Is it not understanding the second list of just syntactic sugar for the next cycle that works?
myTuples = []
for v1 in myList1:
for v2 in myList2:
myTuple = v1,v2
myTuples.append(myTuple)
print myTuples
[('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd')]