comparing two lists

Peter Otten __peter__ at
Thu Aug 23 19:42:09 CEST 2007

Ladislav Andel wrote:

> what would be the most efficient way to do following?
> I have a list of dictionaries  taken from DB e.g.
> dblist = [{'id:1, 'host':'','ip_address':''},
> {'id:3, 'host':'','ip_address':''},
> {'id:9, 'host':'','ip_address':''}]
> and list of object instances in memory(it's just for example)
> which are looping within itself and testing particular hosts
> memlist = [<instance 1>,<instance 2>]
> memlist[0].id  is 1 and  memlist[0].host is etc.
> memlist[1].id  is 9 and  memlist[1].host is etc.
> Now I want to add a new instance to memlist since id=3(in dblist) is not
> in memlist.
> How would you iterate through it and insert a new instance?
> The result should be:
> memlist = [<instance 1>,<instance 2>, <instance 3>]
> memlist[0].id  is 1 and  memlist[0].host is etc.
> memlist[1].id  is 3 and  memlist[1].host is etc.
> memlist[2].id  is 9 and  memlist[2].host is etc.

You should replace the memlist with a dictionary using (host, id) tuples as
the keys. Here's an example that uses a set but requires you to modify the
<instance N> class:

dblist = [{'id':1, 'host':'','ip_address':''},
{'id':3, 'host':'','ip_address':''},
{'id':9, 'host':'','ip_address':''}]

class Item(object):
    def __init__(self, id, host, **discarded):
        self._tuple = (id, host)
    def __hash__(self):
        return hash(self._tuple)
    def __eq__(self, other):
        return self._tuple == other._tuple
    def __repr__(self):
        return "Item(id=%r, host=%r)" % self._tuple

items = set([Item(1, "")])
for d in dblist:
    item = Item(**d)
    if item not in items:
        print "adding", item
        print item, "already there"


More information about the Python-list mailing list