0

On Python I have a list like:

["Years : 1970 - aaaaa", "Month : May - aaaaaaaaa", "Day : 20 - aaaaaaa", "Years : 1971 - aaaaa"]

I just need to extract all items starting by "Years" in another list to have a list like:

["Years : 1970 - aaaaaaa", Years :  1971 - aaaaaaaa"]

How can I achieve this?

dabo248
  • 3,367
  • 4
  • 27
  • 37
anom
  • 1
  • 1
  • Hello and welcome to stackoverflow. Have you checked the website for an answer prior to posting this? I thought that this answer [how to find the python list item that starts with](https://stackoverflow.com/questions/44517191/how-to-find-the-python-list-item-that-start-with) , might have an answer to your question. – Rahul P Feb 17 '20 at 13:31

1 Answers1

0

You can do that with list comprehension:

[i for i in lst if i.startswith('Years')]

Basically, it says For all i elements, return i if it starts with 'Years'

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143