Getting a sublist of a Python list, with the given indices?

You can use list comprehension to get that list:

c = [a[index] for index in b]
print c

This is equivalent to:

c= []
for index in b:
    c.append(a[index])
print c

Output:

[0,2,4,5]

Note:

Remember that some_list[index] is the notation used to access to an element of a list in a specific index.

Leave a Comment

error code: 521