[Tutor] How to find a substring within a list of items

Alan Gauld alan.gauld at btinternet.com
Thu Jan 13 20:42:15 CET 2011


"Richard Querin" <rfquerin at gmail.com> wrote

>I have an object that contains about 3500 list items, each list 
>containing
> various data, some strings and some floats, like so:
>
> ['D', 123.4,'This is a project description', 'type', 52.1,'title']

> What is the easiest way to search this list for a given string?

Is the format constant? In other words are items 2 and 5
always numbers and therefore can be ignored?

If so it might be something as simple as:

result = []
for item in data:
     for index in [0,2,3,5]:
         if 'scrip' in item[index].lower():
             result.append( item)
             break

And you can optimise that as much as you like... :-)

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list