-1

this is nested list

[['Dell', 2], ['Dell', 2], ['HP', 1], ['Sony', 5], ['Sony', 5], ['Sony', 5], ['Sony', 5], ['Apple', 1], ['Sony', 5], ['Apple', 1]]

Output Should be

[['Dell', 2], ['HP', 1], ['Sony', 5], ['Apple', 1],]

3 Answers3

1

If the order doesn't matter:

lis = [['Dell', 2], ['Dell', 2], ['HP', 1], ['Sony', 5], ['Sony', 5], ['Sony', 5], ['Sony', 5], ['Apple', 1], ['Sony', 5], ['Apple', 1]]
result = list(map(list, set(map(tuple, lis))))
print(result)
Frank
  • 1,215
  • 12
  • 24
0

Just iterate through you list and add to other list if it is not already added.

data = [['Dell', 2], ['Dell', 2], ['HP', 1], ['Sony', 5], ['Sony', 5], ['Sony', 5], ['Sony', 5], ['Apple', 1], ['Sony', 5], ['Apple', 1]]
new_data = []
for i in data:
  if i not in new_data:
    new_data.append(i)

print(new_data)
# Output [['Dell', 2], ['HP', 1], ['Sony', 5], ['Apple', 1]]
Prudhvi
  • 1,095
  • 1
  • 7
  • 18
0
before_remove = [['Dell', 2], ['Dell', 2], ['HP', 1], ['Sony', 5], ['Sony', 5], ['Sony', 5], ['Sony', 5], ['Apple', 1], ['Sony', 5], ['Apple', 1]]
remove_duplicates = list(set(tuple(x) for x in before_remove))

if you want the nested items to be lists instead of tuples,

remove_duplicates = [list(y) for y in list(set(tuple(x) for x in before_remove))]
zetamoon
  • 66
  • 5