classification
Title: Syntax to get multiple arbitrary items from an sequence
Type: enhancement Stage: resolved
Components: Versions: Python 3.8
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: amjad ben hedhili, rhettinger, serhiy.storchaka
Priority: normal Keywords:

Created on 2018-03-19 13:36 by amjad ben hedhili, last changed 2018-03-21 15:21 by rhettinger. This issue is now closed.

Messages (5)
msg314095 - (view) Author: AmjadHD (amjad ben hedhili) Date: 2018-03-19 13:36
It will be much of improvement for readability to write:

my_list = ["John", "Richard", "Alice", 1, True, 2.1, "End"]
a, b, c = my_list[1, 3, -1]

instead of:

my_list = ["John", "Richard", "Alice", 1, True, 2.1, "End"]
a, b, c = my_list[1], my_list[3], my_list[-1]
msg314098 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-03-19 15:48
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
msg314125 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-03-20 01:12
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])
>>> a
'Richard'
>>> b
1
>>> c
'End'
msg314195 - (view) Author: AmjadHD (amjad ben hedhili) Date: 2018-03-21 13:21
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 python.
msg314203 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2018-03-21 15:21
Please leave this closed -- there is no chance that this will go forward on the bug tracker.  It would first need to be thoroughly discussed on the python-ideas mail list.
History
Date User Action Args
2018-03-21 15:21:37rhettingersetstatus: open -> closed

versions: + Python 3.8
messages: + msg314203
title: Syntax to get multiple arbitrary items from an iterable -> Syntax to get multiple arbitrary items from an sequence
2018-03-21 13:21:16amjad ben hedhilisetstatus: closed -> open

messages: + msg314195
2018-03-20 01:12:25rhettingersetnosy: + rhettinger
messages: + msg314125
2018-03-19 15:48:09serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg314098

resolution: rejected
stage: resolved
2018-03-19 13:36:49amjad ben hedhilisettitle: Syntax to get multiple items from an iterable -> Syntax to get multiple arbitrary items from an iterable
2018-03-19 13:36:19amjad ben hedhilicreate