<br><br><div class="gmail_quote">On Mon, Jul 5, 2010 at 11:58 PM, Shashwat Anand <span dir="ltr"><<a href="mailto:anand.shashwat@gmail.com">anand.shashwat@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br><br><div class="gmail_quote"><div class="im">On Mon, Jul 5, 2010 at 11:24 PM, Vineeth Rakesh <span dir="ltr"><<a href="mailto:vineethrakesh@gmail.com" target="_blank">vineethrakesh@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hello all,<br><br>Can some one help me to return a special pattern from a list.<br><br>say list = ["something1.mp3","something2.mp3","something4.pdf","something5.odt"]<br></blockquote>
<div><br></div></div><div>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.</div><div class="im"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>now say I just need to return the files with .mp3 extension. How to go about doing this?<br></blockquote><div><br></div><div>>>> list = ["something1.mp3","something2.mp3","something4.pdf","something5.odt"]</div>
</div><div>>>> [i for i in list if i[-4:] == '.mp3']</div><div>['something1.mp3', 'something2.mp3']</div><div><br></div><div>or may be ,</div><div>>>> [i for i in list if os.path.splitext(i)[0] == '.mp3'] # If you want to deal with file extentions, that is.</div>
</div></blockquote><div><br></div><div>Oops, sorry for the typo.</div><div>It'll be , </div><div>>>> [i for i in list if os.path.splitext(i)[1] == '.mp3'] </div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="gmail_quote">
<div>For smaller case string is good, for obscure patter, you can try regex module.</div><div><br></div><div> ~l0nwlf</div></div>
</blockquote></div><br>