Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the python list item that start with

Tags:

python

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_%'

like image 664
Learnings Avatar asked Jun 13 '17 09:06

Learnings


2 Answers

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)
like image 142
temasso Avatar answered Oct 01 '22 01:10

temasso


You should try something like this :

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

where "my_list" is your list of items.

like image 28
RyanU Avatar answered Oct 01 '22 02:10

RyanU