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) *  |
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) *  |
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) *  |
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. |
|
|