-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslice_by_index.py
More file actions
45 lines (36 loc) · 1.05 KB
/
slice_by_index.py
File metadata and controls
45 lines (36 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3
# @file: slice_by_index.py
# @auth: sprax
# @date: 2020-04-03 01:37:23 Fri 03 Apr
'''
https://stackoverflow.com/questions/9106065/python-list-slicing-with-arbitrary-indices
'''
from operator import itemgetter
def slice_by_index(lst, indexes):
"""Slice list by positional indexes.
Adapted from https://stackoverflow.com/a/9108109/304209.
Args:
lst: list to slice.
indexes: iterable of 0-based indexes of the list positions to return.
Returns:
a new list containing elements of lst on positions specified by
indexes.
>>> slice_by_index([], [])
[]
>>> slice_by_index([], [0, 1])
[]
>>> slice_by_index(['a', 'b', 'c'], [])
[]
>>> slice_by_index(['a', 'b', 'c'], [0, 2])
['a', 'c']
>>> slice_by_index(['a', 'b', 'c'], [0, 1])
['a', 'b']
>>> slice_by_index(['a', 'b', 'c'], [1])
['b']
"""
if not lst or not indexes:
return []
slice_ = itemgetter(*indexes)(lst)
if len(indexes) == 1:
return [slice_]
return list(slice_)