AmjadHD added the comment:
Yes that's a way to do it but "a, b, c = my_list[1, 3, -1]" seems so pythonic
and straight forward, it's like formatting, python had already 3 methods to do
it when it introduced a 4th one (f-strings), easier is better especially in
Raymond Hettinger added the comment:
FWIW, there is already a way to do this but it involves the extra step of
applying map() to a bound method:
>>> my_list = ["John", "Richard", "Alice", 1, True, 2.1, "End"]
>>> a, b, c = map(my_list.__getitem__, [1, 3, -1])
>>>
Serhiy Storchaka added the comment:
This syntax already is supported for dicts and NumPy arrays, but with different
semantic.
>>> d = {(1, 2): 'foo'}
>>> d[1, 2]
'foo'
>>> a = numpy.array([[1, 2], [3, 4]])
>>> a[1, 0]
3
--
nosy: +serhiy.storchaka
Change by amjad ben hedhili :
--
title: Syntax to get multiple items from an iterable -> Syntax to get multiple
arbitrary items from an iterable
___
Python tracker