I have some functions which retrive lists of items, matrix of itens. after retrieving a matrix I need to put the itens in a new list, and avoid duplicated ones. With nested for loops it´s easy, but I would like to know how to perform the same with list comprehension. My problem is being to put the condition to avoid to insert the duplicated one: something like this:
pseudocode:
new= [['Captain Marvel', 'Avengers: Infinity War', 'Ant-Man And The Wasp', 'The Fate Of The Furious', 'Deadpool 2'], ['Inhumans', 'The Fate Of The Furious', 'Venom', 'American Assassin', 'Black Panther']]
lista2 =[]
for movieL in new:
lista2 = [val
for sublist in new
for val in sublist
#if val not in lista2 this does not work
]
Result:
['Captain Marvel', 'Avengers: Infinity War', 'Ant-Man And The Wasp', 'The Fate Of The Furious', 'Deadpool 2', 'Inhumans', 'The Fate Of The Furious', 'Venom', 'American Assassin', 'Black Panther']
If sorting matters:
new = [['Captain Marvel', 'Avengers: Infinity War', 'Ant-Man And The Wasp', 'The Fate Of The Furious', 'Deadpool 2'], ['Inhumans', 'The Fate Of The Furious', 'Venom', 'American Assassin', 'Black Panther']]
lst = [item for sublist in new for item in sublist]
print(sorted(list(set(lst)), key=lambda x: lst.index(x)))
If it doesn't matter:
new = [['Captain Marvel', 'Avengers: Infinity War', 'Ant-Man And The Wasp', 'The Fate Of The Furious', 'Deadpool 2'], ['Inhumans', 'The Fate Of The Furious', 'Venom', 'American Assassin', 'Black Panther']]
print(list(set([item for sublist in new for item in sublist])))