Skip to content
Advertisement

select top 3 unique items in a list of list

I have a list of items and it outputs a list of list:

item_list = ['book', 'pen']
output = [
    ['pencil', 'eraser', 'pen', 'glue', 'folders'], 
    ['notepad', 'book','pencil', 'markers','ruler','scissors']
]

Is there a way to get the output as a unique list of items that contains the top 3 items from each sublist but also not in the initial item_list?

so that the output will look like (in the exact order):

new_output = ['pencil', 'eraser', 'glue', 'notepad', 'markers', 'ruler']

or if possible:

new_output = ['pencil', 'notepad', 'eraser', 'markers', 'glue','ruler']

Advertisement

Answer

You must keep a set of already seen items to find the unique ones. A trivial implementation would be:

item_list = ['book', 'pen']
output = [['pencil', 'eraser', 'pen', 'glue', 'folders'],
          ['notepad', 'book', 'pencil', 'markers', 'ruler', 'scissors']]

already_seen = set(item_list)
new_output = []
for sublist in output:
  new_sublist = [item for item in sublist if item not in already_seen]
  new_output.extend(new_sublist[:3])
  already_seen |= set(new_sublist[:3])

print(new_output)

output:

['pencil', 'eraser', 'glue', 'notepad', 'markers', 'ruler']
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement