common problem - elegant solution sought

Neil Cerutti mr.cerutti at gmail.com
Tue Jan 15 08:13:44 EST 2008


On Jan 15, 2008 5:33 AM, Helmut Jarausch <jarausch at igpm.rwth-aachen.de> wrote:
> Hi,
>
> I'm looking for an elegant solution of the following tiny but common problem.
>
> I have a list of tuples  (Unique_ID,Date) both of which are strings.
> I want to delete the tuple (element) with a given Unique_ID, but
> I don't known the corresponding Date.
>
> My straight forward solution is a bit lengthy, e.g.
>
> L=[("a","070501"),("b","080115"),("c","071231")]

If the data is truly sorted by Unique_ID, then binary search may be
feasible (though not actually recommended for such small list).

import bisect

i = bisect.bisect_left(L, ('b', '000000'))
if i[0] == 'b':
    del L[i]

-- 
Neil Cerutti <mr.cerutti+python at gmail.com>



More information about the Python-list mailing list