How can I remove duplicates from this list? x = [name, code]
Some elements of list has the same code: list = [['cafe', '/fr/48349066'], ['cafe', '/fr/48349056'], ['cafe', '/fr/48349066']]
How can I remove duplicates from this list? x = [name, code]
Some elements of list has the same code: list = [['cafe', '/fr/48349066'], ['cafe', '/fr/48349056'], ['cafe', '/fr/48349066']]
There's probably a more processing-efficient way to do this, but this is the way I've always done stuff like this:
for i in range(len(list)):
for j in range(len(list)):
if list[i][1] == list[j][1]:
list.pop(i)
There is a lot of ways to removing duplicates;
1-) The Naive Method;
new_list = []
for i in some_list:
if i not in new_list:
res.append(i)
2-) Using list comprehension;
new_list = []
[new_list.append(x) for x in some_list if x not in new_list]
3-) Using set();
some_list = list(set(some_list))
4-) Using collections.OrderedDict.fromkeys()
from collections import OrderedDict
new_list = list(OrderedDict.fromkeys(some_list))