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
Jan 30 at 11:39





More elegant, more simplistic than what? Could you please share your attempts?
– CristiFati
Jan 30 at 11:40





Possible duplicate of Get unique values from a nested list in python
– Santhosh
Jan 30 at 12:07




3 Answers
3



Here is one solution. It is often better to store your data in a more structured form, e.g. a dictionary, rather than manipulate from one list format to another.


from collections import defaultdict

list_all = [[['some_item'],'Robert'],
[['another_item'],'Robert'],
[['itemx'],'Adam'],
[['item2','item3'],'Maurice']]

d = defaultdict(list)

for i in list_all:
d[i[1]].extend(i[0])

# defaultdict(list,
# {'Adam': ['itemx'],
# 'Maurice': ['item2', 'item3'],
# 'Robert': ['some_item', 'another_item']})

d2 = [[v, k] for k, v in d.items()]

# [[['some_item', 'another_item'], 'Robert'],
# [['itemx'], 'Adam'],
# [['item2', 'item3'], 'Maurice']]





@PerlPingu sadly yes, I too was thinking of storing it into a dict,but I'm currently constrained to this. But jp_data_analysis answer is spot on.
– HaR
Jan 30 at 12:06



You can try this, though it's quite similar to above answer but you can do this without importing anything.


list_all = [[['some_item'], 'Robert'], [['another_item'], 'Robert'], [['itemx'], 'Adam'], [['item2', 'item3'], 'Maurice']]

x = {} # initializing a dictionary to store the data

for i in list_all:
try:
x[i[1]].extend(i[0])
except KeyError:
x[i[1]] = i[0]

list2 = [[j, i ] for i,j in x.items()]


list_all = [[['some_item'],'Robert'] ,[['another_item'],'Robert'],[['itemx'],'Adam'],[['item2','item3'],'Maurice']]

dict_value = {}
for val in list_all:
list_, name = val
if name in dict_value:
dict_value[name][0].extend(list_)
else:
dict_value.setdefault(name,[list_, name])
print(list(dict_value.values()))
>>>[[['some_item', 'another_item'], 'Robert'],
[['itemx'], 'Adam'],
[['item2', 'item3'], 'Maurice']]






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV