Posts

Showing posts with the label nested-lists

Append list based on another element in list and remove lists that contained the items

Append list based on another element in list and remove lists that contained the items Let's say I have two lists like this: list_all = [[['some_item'],'Robert'] ,[['another_item'],'Robert'],[['itemx'],'Adam'],[['item2','item3'],'Maurice]] I want to combine the items together by their holder (i.e 'Robert') only when they are in separate lists. Ie in the end list_all should contain: list_all = [[['some_name','something_else'],'Robert'],[['itemx'],'Adam'],[['item2','item3'],'Maurice]] What is a fast and effective way of doing it? I've tried in different ways but I'm looking for something more elegant, more simplistic. Thank you Is keeping the results in this list format required for some reason? I would have thought a dict of lists with names as keys may make more sense – PerlPingu ...