Blueboots
Blueboots

Reputation: 59

Using a list of lists containing indexes to pull strings by index from another list

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

Answers (1)

Andrey
Andrey

Reputation: 400

[[list_of_text[idx-1] for idx in indices] for indices in list_of_indexes]

Upvotes: 1

Related Questions