Another way: from collections import defaultdict
x = [['cat', 'NM123', 12], ['cat', 'NM234', 12], ['dog', 'NM56', 65]] y = [['cat', 'NM123, NM234', 12], ['dog', 'NM56', 65]] def f(acc, x): acc[(x[0], x[2])] += [x[1]] return acc assert y == [[k[0], ', '.join(v), k[1]] for k, v in reduce(f, x, defaultdict(list)).iteritems()] On Thu, Dec 5, 2013 at 3:00 PM, Nitin Kumar <nitin.n...@gmail.com> wrote: > long but this too works :) > > >>> p=[] > >>> x=[['cat','NM123',12],['cat','NM234',12], ['dog', 'NM56',65]] > >>> import copy > >>> def fn(a,b): > if len(a)==3 and a[0]==b[0] and a[-1]==b[-1] and a[1]!=b[1]: > a.insert(-1,b[1]) > p.append(a) > else: > p.append(b) > return p > > >>> reduce(fn, copy.deepcopy(x)) > [['cat', 'NM123', 'NM234', 12], ['dog', 'NM56', 65]] > >>> p > [['cat', 'NM123', 'NM234', 12], ['dog', 'NM56', 65]] > >>> > >>> > >>> x > [['cat', 'NM123', 12], ['cat', 'NM234', 12], ['dog', 'NM56', 65]] > >>> 'p is your y' > > Nitin K > > > On Thu, Dec 5, 2013 at 1:55 PM, Vikram K <vikthirtyf...@gmail.com> wrote: > > > Any suggestions on what i have to do to go from x to y? > > > > >>> x = [['cat','NM123',12],['cat','NM234',12], ['dog', 'NM56',65]] > > >>> x > > [['cat', 'NM123', 12], ['cat', 'NM234', 12], ['dog', 'NM56', 65]] > > >>> y = [] > > >>> y = [['cat','NM123, NM234', 12], ['dog', 'NM56', 65]] > > >>> > > > > Regards > > Vik > > _______________________________________________ > > BangPypers mailing list > > BangPypers@python.org > > https://mail.python.org/mailman/listinfo/bangpypers > > > _______________________________________________ > BangPypers mailing list > BangPypers@python.org > https://mail.python.org/mailman/listinfo/bangpypers > -- Vineet Naik _______________________________________________ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers