list comprehension in place of nested loop avoiding item duplicity

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']

Upvotes: 0

Views: 86

Answers (3)

revliscano
revliscano

Reputation: 2272

If keeping the original order is not important in the result, you can take advantage of sets, and use a set union operation:

from functools import reduce


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']]
result = [*reduce(set.union, map(set, new))]
print(result)
#Outputs ['Captain Marvel', 'Venom', 'Black Panther', 'Ant-Man And The Wasp', 'American Assassin', 'Inhumans', 'Deadpool 2', 'Avengers: Infinity War', 'The Fate Of The Furious']

Alternatively, if you strictly need to use a comprehension syntax (in this case generator comprehension), you could use:

result = [*set(item for list_ in new for item in list_)]

Upvotes: 1

Andreas
Andreas

Reputation: 9207

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])))

Upvotes: 0

Chris
Chris

Reputation: 16172

You can use itertools.chain.from_iterable() to flatten all of your lists into a single one, and apply the set() to remove duplicates.

from itertools import chain
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']]
set(chain.from_iterable(new))


{'Captain Marvel', 'Avengers: Infinity War', 'The Fate Of The Furious', 'Inhumans', 'Ant-Man And The Wasp', 'Black Panther', 'Deadpool 2', 'Venom', 'American Assassin'}

Upvotes: 0

Related Questions