0

My goal is to extract an element from many list that similar like this. Taking elements that is food.

test_list =
['Tools: Pen',
'Food: Sandwich',
'Fruit: Apple'
]

I the final result would be "Sandwich" by look list element with the word "Food:" and split from there.

My usual method is lookup by index

test_list[1].split(': ')[1]

However, I go through many lists that number of elements is varied, and I want to look up the food item in the "food:" section. I don't know if there is a function that would help me choose element from a list using "keyword search"

Another example,

test_list_2 =
['Tools: Pen',
'Tree: Willow'
'Food: Drumstick',
'Fruit: Apple'
]

With the same lines of code, test_list_2 result would be "Drumstick"

Please kindly help my find a way to do that in Python. Thank you

Hay Team
  • 3
  • 1

2 Answers2

2

I would consider using a dictionary for this application.

to translate a list of the form you gave into a dictionary:

test_dict = {i.split(": ")[0]: i.split(": ")[1] for i in test_list}

And then you can access elements by key

test_dict['Food']
Iddo Sadeh
  • 133
  • 6
  • Hi, what if this list is parsed from a pdf. I can't change it or make a dictionary. Is there any way to lookup an element without using dictionary? – Hay Team Nov 19 '22 at 00:40
  • 1
    You can view the answer from @Daniel Hao in this case. May I ask how you are parsing the pdf? – Iddo Sadeh Nov 19 '22 at 00:43
  • @HayTeam - one way is to loop as I've suggested in the post. – Daniel Hao Nov 19 '22 at 00:44
  • You can also use list comprehension to translate a list to a dictionary. I will update my answer with this solution. – Iddo Sadeh Nov 19 '22 at 00:46
  • I use pdfplumber and use .split(\n) on the pdf. and I only parse the info in each list element. Idk if that make sense or the most effective way to parse pdf. I parse multiple pdf using a loop as well. most pdf has same format, but some has extra space/notes. and it messed up the code if i use index to find a specific info – Hay Team Nov 19 '22 at 00:47
  • @HayTeam can you give an example of when the indexing doesn't work? – Iddo Sadeh Nov 19 '22 at 00:58
  • compare between test_list vs test_list_2, Food is index as [1] in test_list and as [2] in test_list_2. so if i apply LIST[1].split(': ')[1] it would not work for all the list. – Hay Team Nov 19 '22 at 01:04
  • 1
    @IddoSadeh i looked up list comprehension and it work perfect for my application. thank you – Hay Team Nov 19 '22 at 01:05
0

What you want is a dictionary.

test_list_2 =
{'Tools': 'Pen',
'Tree': 'Willow',
'Food': 'Drumstick',
'Fruit': 'Apple'
}

print(test_list_2["Tools"])
QWERTYL
  • 1,355
  • 1
  • 7
  • 11
  • Hi, what if this list is parsed from a pdf. I can't change it or make a dictionary. Is there any way to lookup an element without using dictionary? – Hay Team Nov 19 '22 at 00:40
  • "I can't change it or make it a dictionary" -- Yes you can, just split it using commas as you were doing before and do `test_list_2[key_from_list] = value_from_list` – QWERTYL Nov 19 '22 at 00:46
  • 1
    @HayTeam you can definitely make it a dictionary. – juanpa.arrivillaga Nov 19 '22 at 00:49