[BangPypers] How for and list.remove() works

Anand Balachandran Pillai abpillai at gmail.com
Thu Jul 10 10:59:25 CEST 2008


Hi Kiran,

    Sets can be quite useful in situations where you want to remove
redundant items from a Python container. Another often employed
trick is to convert the container to dictionary keys which automatically
drops duplicate items - this is the trick in my 2nd solution.

--Anand

On Thu, Jul 10, 2008 at 2:17 PM, Kiran Jonnalagadda <jace at pobox.com> wrote:
> That's rather clever, Anand. My hackish solution to this sort of thing has
> usually been:
>
> while 12 in a:
>  a.remove(12)
>
> Which I've normally used when removing blank strings from a recently split
> string.
>
>
> --
> Kiran Jonnalagadda
> http://jace.seacrow.com/
> http://jace.livejournal.com/
>
> On 10-Jul-08, at 2:03 PM, Anand Balachandran Pillai wrote:
>
>> Here is what is arguably the solution with the least code.
>>
>>>>> a =  [12, 12, 1321, 34, 23, 12, 34, 45, 77]
>>>>> list(set(a)-set([12]))
>>
>> [1321, 34, 23, 45, 77]
>>
>>
>> Cheers
>>
>> --Anand
>
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>



-- 
-Anand


More information about the BangPypers mailing list