-1

I have a list of strings in which there are a lot of repeated items. I would like to make a new list in which all items are present but there is only one occurrence of each item.

input:

mylist = ["hg", "yt", "hg", "tr", "yt"]

output:

newlist = ["hg", "yt", "tr"]

I actually have tried this code but did not return what I want:

newlist = []
for i in range(len(mylist)):
    if mylist[i+1] == mylist[i]:
        newlist.append(mylist[i])
Clock Slave
  • 7,627
  • 15
  • 68
  • 109
user3631908
  • 23
  • 1
  • 8

1 Answers1

2

You can simply use a set:

newlist = set(mylist)

Or, to retrieve exactly a list, but is can be useless depending what you are doing with:

nexlist = list(set(mylist))    
Tiger-222
  • 6,677
  • 3
  • 47
  • 60