i curious know if there "pythonic" way assign values in list elements? clearer, asking this:
mylist = [3, 5, 7, 2] a, b, c, d = something(mylist) so that:
a = 3 b = 5 c = 7 d = 2 i looking other, better option doing manually:
a = mylist[0] b = mylist[1] c = mylist[2] d = mylist[3]
simply type out:
>>> a,b,c,d = [1,2,3,4] >>> 1 >>> b 2 >>> c 3 >>> d 4 python employs assignment unpacking when have iterable being assigned multiple variables above.
in python3.x has been extended, can unpack number of variables less length of iterable using star operator:
>>> a,b,*c = [1,2,3,4] >>> 1 >>> b 2 >>> c [3, 4]
Comments
Post a Comment