comparing two lists

Ladislav Andel ladaan at
Thu Aug 23 18:27:40 CEST 2007

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.

Furthermore, I can have the opposite situation.
This time I need to remove from memlist a host which is not in dblist.
How would you do this?

The way how it works is that DBlist is loaded every 10 minutes and 
compares with memlist.
The memlist should be the same as dblist.

Could you help me, please?
I'm working on my version of this but somebody might be quicker than me.
In case I have it done I will post it.


There is

and wt

More information about the Python-list mailing list