Compact way to assign values by slicing list in Python

You can use operator.itemgetter:

>>> from operator import itemgetter
>>> bar = ['a','b','c','x','y','z']
>>> itemgetter(0, 3, 4)(bar)
('a', 'x', 'y')

So for your example you would do the following:

>>> v1, v2, v3 = itemgetter(0, 3, 4)(bar)

Leave a Comment