0

I have this list in Python:

fruit = ['apple','grape','watermelon','pineapple']

And another list like that:

idx_list = [1,3]

Now, I need to match each value from idx_list with the indexes in the fruit to get a new list with values from fruit list using list comprehension.

Like this:

['grape','pineapple']

I'm trying to use this command [f for f in fruit.columns] but I'm only getting the values from fruit.

Rafael Lima
  • 420
  • 1
  • 5
  • 16
  • Why did you expect `fruit.columns` to be a thing? Lists don't have a `columns` attribute, and `fruit` doesn't even store columnar data. Are you getting things mixed up with a different data type? – user2357112 Dec 20 '20 at 22:02
  • Because I needed the column names from fruit based in the values into idx_list. Like the answer bellow. – Rafael Lima Dec 20 '20 at 22:05

1 Answers1

1

try this:

fruit = ['apple','grape','watermelon','pineapple']
idx_list = [1,3]
print([fruit[i] for i in idx_list])
dimay
  • 2,768
  • 1
  • 13
  • 22