You can use indexing to access the elements of a list in python. If you have a list of indices, you can get the elements corresponding to those indices using list comprehension. Here's an example:
main.py145 chars5 lines
Output:
main.py32 chars2 lines
Here, we have a list my_list
and a list of indices indices
. We use list comprehension to create a new list output_list
which contains the elements of my_list
at the indices specified in indices
. The output is ['apple', 'cherry', 'eggfruit']
.
gistlibby LogSnag