J
jimgardener
hi
I have two lists of names.I need to find the difference between these
two lists.I tried to do it using sets.But I am wondering if there is a
better way to do it.Please tell me if there is a more elegant way.
thanks,
jim
my code snippet follows..
oldlst=['jon','arya','ned','bran']
newlst=['jaime','jon','cersei']
newlyadded=set(newlst)-set(oldlst)
removed=set(oldlst)-set(newlst)
unchanged=set(oldlst)& set(newlst)
print '%d were newly added= %s'%(len(newlyadded),list(newlyadded))
print '%d were removed=%s'%(len(removed),list(removed))
print '%d were unchanged=%s'%(len(unchanged),list(unchanged))
this produces the output
I have two lists of names.I need to find the difference between these
two lists.I tried to do it using sets.But I am wondering if there is a
better way to do it.Please tell me if there is a more elegant way.
thanks,
jim
my code snippet follows..
oldlst=['jon','arya','ned','bran']
newlst=['jaime','jon','cersei']
newlyadded=set(newlst)-set(oldlst)
removed=set(oldlst)-set(newlst)
unchanged=set(oldlst)& set(newlst)
print '%d were newly added= %s'%(len(newlyadded),list(newlyadded))
print '%d were removed=%s'%(len(removed),list(removed))
print '%d were unchanged=%s'%(len(unchanged),list(unchanged))
this produces the output