Reputation: 59
I have a list of lists that contain integers. These integers are indexes for each element in the list of strings. I need to use the list of indexes to select the correct string from the list of strings; creating a new list of lists of the just the selected strings. Sorry for the tongue twister of an explanation, I tried to make it is clear as possible. Below is a simple example of what I am trying to achieve.
list_of_indexes=[[0,2,4],[5,7,6],[1,9]]
list_of_text=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
desired_output = [['a','c','e'], ['f', 'h', 'g'], ['b', 'j']]
Upvotes: 0
Views: 1231
Reputation: 400
[[list_of_text[idx-1] for idx in indices] for indices in list_of_indexes]
Upvotes: 1