-1

I want the unique list from the following list of list.

A = [[-1, 0, 1], [-1, -1, 2], [-1, 0, 1]]
output = [[-1,0,1], [-1,-1,2]] 

I don't want to use any package. could you help me with that?

2 Answers2

0

You can use set to get a unique list from number of lists

unique_data = [list(x) for x in set(tuple(x) for x in A)]

This will generate your expected output. :)

Parth Shah
  • 128
  • 1
  • 9
  • @Meskaj Can you please accept the answer so if anyone who in search of same question then he/she will get to know that solutions is worked. Thank you – Parth Shah Jul 22 '21 at 05:45
0

The other answer is correct but it doesn't maintain the original list order.

In case the order is important to you :

A = [['a', 0, 1], [-1, -1, 2], ['a', 0, 1]]
res = [list(i) for i in dict.fromkeys(map(tuple, A))]
print(res)

output :

[['a', 0, 1], [-1, -1, 2]]

Note : I've put 'a' inside the sub lists in order to show you that in several runs you will get different result by using sets in other solution. Dictionaries maintain the insertion order now.

S.B
  • 13,077
  • 10
  • 22
  • 49