-1

I have several lists of dicts showing a word and it's position in a sentence (start/end character). However, some words are repeated, but their position in the sentence varies, so I can't find a way to find the duplicates. How can I remove those words from my list of dictionaries?

[{'word': 'sun',
 'start': 10,
 'end': 14},
{'word': 'earth',
 'start': 20,
 'end': 26},
{'word': 'sun',
 'start': 30,
 'end': 34}]

so I was hoping to get

[{'word': 'sun',
 'start': 10,
 'end': 14},
{'word': 'earth',
 'start': 20,
 'end': 26}]

Thanks

Cris
  • 15
  • 2

1 Answers1

0

Let's say your list is called l. Then you could use:

alreadyUsed = list()
stuffToDelete = list()
for d in l:
    word = d['word']
    if word in alreadyUsed:
        stuffToDelete.append(d)
    else:
        alreadyUsed.append(word)
for d in stuffToDelete:
    del l[l.index(d)]
Jacob
  • 225
  • 1
  • 9
  • 1
    Thanks for your comment, but I get an error saying: `ValueError: '‘pluto' is not in list', and 'pluto' is definitely one of the repeated words :( – Cris Jun 13 '19 at 14:33
  • Sorry, I asked the list to search for the actual word, not the dictionary that contains the word. I'll edit my post. – Jacob Jun 13 '19 at 14:44