Reputation: 13
I have a nested list, like
list1 = [['banana', 'apple', 'orange', 'tomato'], ['apple', 'orange', 'tomato', 'banana'], ['orange', 'tomato', 'banana', 'apple'], ['banana', 'apple', 'orange', 'tomato'], ['apple', 'orange', 'tomato', 'banana']]
how do I calculate the unique quantity? In this case, there are 3 unique nested lists: ['banana', 'apple', 'orange', 'tomato'], ['apple', 'orange', 'tomato', 'banana'], ['orange', 'tomato', 'banana', 'apple']
In fact, the length of the nested list will be from two to three thousand
Upvotes: 1
Views: 45
Reputation: 71424
If you convert each list to a tuple
, you can put them all in a set
which automatically eliminates the duplicates:
>>> {tuple(fruits) for fruits in list1}
{('orange', 'tomato', 'banana', 'apple'), ('apple', 'orange', 'tomato', 'banana'), ('banana', 'apple', 'orange', 'tomato')}
If you want to get that back into the list-of-lists form:
>>> [list(fruits) for fruits in {tuple(fruits) for fruits in list1}]
[['orange', 'tomato', 'banana', 'apple'], ['apple', 'orange', 'tomato', 'banana'], ['banana', 'apple', 'orange', 'tomato']]
Upvotes: 6