How to access multiple elements of list knowing their index in Python?
Example-1
thelist = [2, 11, 8, 9, 4, 1, 3] items = [2, 4, 5] elements = [e for i, e in enumerate(thelist) if i in items] print(elements)
[8, 4, 1]
Example-2
thelist = [2, 11, 8, 9, 4, 1, 3] items = [2, 4, 5] elements = [x for x in thelist if thelist.index(x) in items] print(elements)
[8, 4, 1]