While looking at some Python code I noticed usage of what looked like ,=
operator:
a ,= b
After experimentation and very close ins
The statement;
a, = b
a = b[0]
)len(b) == 1
)In that sense it is not the same as a = b[0]
. The latter would work with b = ['a', 'b']
, while the first will raise a ValueError: too many values to unpack
. On the other side:
l = [x]
it = iter(l)
a, = it #fine when a=it[0] raises TypeError: 'listiterator' object has no attribute '__getitem__'
TL/DR: if b is a list or a tuple with one single element, both a, = b
and a = b[0]
behave the same, but in other cases the may behave differently