[Tutor] Help return a pattern from list

Shashwat Anand anand.shashwat at gmail.com
Mon Jul 5 20:28:38 CEST 2010

On Mon, Jul 5, 2010 at 11:24 PM, Vineeth Rakesh <vineethrakesh at gmail.com>wrote:

> Hello all,
> Can some one help me to return a special pattern from a list.
> say list =
> ["something1.mp3","something2.mp3","something4.pdf","something5.odt"]

One suggestion. Don't name a list as list. Use l or List or any other
variable name. list is one of the syntax in python.

> now say I just need to return the files with .mp3 extension. How to go
> about doing this?

>>> list =
>>> [i for i in list if i[-4:] == '.mp3']
['something1.mp3', 'something2.mp3']

or may be ,
>>> [i for i in list if os.path.splitext(i)[0] == '.mp3']  # If you want to
deal with file extentions, that is.
For smaller case string is good, for obscure patter, you can try regex

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100705/914abc94/attachment-0001.html>

More information about the Tutor mailing list