Helmut Jarausch wrote: > 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")] > pos=-1 > found=-1 > for (Key,Date) in L : > pos+= 1 > if Key == "b" : > found= pos > break > > if found >= 0 : > del L[found] > > print L > > Most probably there are much more elegant solutions. > Unfortunately, the index-list-method doesn't take an > additional function argument for the comparisons.
Probably the most common solution to this in Python is to produce a second list which has all the items in the first except for the one(s) you wish to delete: <code> L=[("a","070501"),("b","080115"),("c","071231")] L2 = [(uniqid, date) for (uniqid, date) in L if not uniqid == 'b'] </code> It might look a little wasteful, but since Python lists are supremely fast and since the tuples themselves aren't copied, only their references, the result is probably what you need. Obviously you've given us a toy example, which is fine for demonstrating the problem. Suggestions might vary if, for example, your data set were much bigger or if the tuples were more complex. TJG -- http://mail.python.org/mailman/listinfo/python-list