[Tutor] Removing itens from a list

Andy W toodles@yifan.net
Wed, 16 Jan 2002 02:00:04 +0800


> Hi. This is my first message to this great list. Wish I find out about
> you before :) Sorry for my english, we speak portuguese here.

Hi Marco!
Welcome to the list.

>
>
> My question is:
>
> In the following list,how do I remove every item with "orange" on it?
> l = ['I like Orange juice','orange','OranGes are good','I like apple']
>
>
> I think I should use re module, but dont now how to use it.
> Do you know any site that explain the use of re module or regex with
python ?

Using Regular Expressions is not necessary.
Try the following:

#
l = ['I like Orange juice','orange','OranGes are good','I like apple']

for item in l[:]:                              #Make a copy of the list, and
iterate through it.
  if item.lower().count("orange"):   #Get the string in lowercase, and count
the number of times "orange" appears.
    l.remove(item)                          #If the count > 0, remove the
item.
#

But here's a HOWTO for Regular Expressions anyway.
http://py-howto.sourceforge.net/regex/regex.html

HTH,
Andy

>
> Thanks in advance, and keep up the good work. You guys are *really* making
> a difference.
>
> --
> Buy a copy of a baby naming book and you'll never be at a loss for
> variable names.
> - How To Write Unmaintainable Code
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>