-2

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

azsds
  • 1
  • Does this answer your question? [Python list subtraction operation](https://stackoverflow.com/questions/3428536/python-list-subtraction-operation) – Diggy. May 18 '20 at 20:00
  • @Diggy I tried set(x) and it doesn't work – azsds May 18 '20 at 20:06
  • 1: https://stackoverflow.com/questions/7961363/removing-duplicates-in-lists 2: https://stackoverflow.com/questions/35731289/function-to-remove-duplicates-from-a-list-python 3: https://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-whilst-preserving-order – pacukluka May 18 '20 at 20:44
  • Please post a sample of what you've tried so far, and we'll try to help you. – David Hempy May 19 '20 at 03:09

2 Answers2

1

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)
Sanford Bassett
  • 328
  • 2
  • 11
0

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)) 
Yagiz Degirmenci
  • 16,595
  • 7
  • 65
  • 85