common problem - elegant solution sought
cokofreedom at gmail.com
cokofreedom at gmail.com
Tue Jan 15 05:50:17 EST 2008
> 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")]
Do they have to be tuples? Seems to me if you are using a Unique_ID
that a dictionary would be perfect. Indeed when you are looking to
delete an entry you can simply look for the Unique_ID in the
dictionary and it will be a very fast look up.
if key in "my_dictionary":
However, if you must use a list of tuples, your current method is very
inefficient. Why not use the del within the if Key == "b":?
I cannot provide extremely elegant solutions with your current system,
but I can tell you with a large enough list your way is going to take
a very long time since you will iterate over the whole list
sequentially to find an entry...
More information about the Python-list
mailing list