How to find the python list item that start with

Question:

I have the list that contains some items like:

"GFS01_06-13-2017 05-10-18-38.csv"
"Metadata_GFS01_06-13-2017 05-10-18-38.csv"

How to find the list item that start with "GFS01_"

In SQL I use query: select item from list where item like 'GFS01_%'

Asked By: Learnings

||

Answers:

You should try something like this :

[item for item in my_list if item.startswith('GFS01_')]

where “my_list” is your list of items.

Answered By: RyanU

You have several options, but most obvious are:

Using list comprehension with a condition:

result = [i for i in some_list if i.startswith('GFS01_')]

Using filter (which returns iterator)

result = filter(lambda x: x.startswith('GFS01_'), some_list)
Answered By: temasso

If you really want the string output like this “GFS01_06-13-2017 05-10-18-38.csv”,”GFS01_xxx-xx-xx.csv”, you could try this:

', '.join([item for item in myList if item.startswith('GFS01_')])

Or with quotes

', '.join(['"%s"' % item for item in myList if item.startswith('GFS01_')])

Filtering of list will gives you list again and that needs to be handled as per you requirement then.

Answered By: Senthilkumar C
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.