Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing duplicate strings from a list in python [duplicate]

Tags:

python

string

If I have a string list ,

a = ["asd","def","ase","dfg","asd","def","dfg"]

how can I remove the duplicates from the list?

like image 397
Shamika Avatar asked Nov 20 '11 08:11

Shamika


People also ask

How do you remove duplicates from a list of strings in Python?

You can remove duplicates from a Python using the dict. fromkeys(), which generates a dictionary that removes any duplicate values. You can also convert a list to a set. You must convert the dictionary or set back into a list to see a list whose duplicates have been removed.

How do you remove duplicate text in Python?

We use file handling methods in python to remove duplicate lines in python text file or function. The text file or function has to be in the same directory as the python program file. Following code is one way of removing duplicates in a text file bar. txt and the output is stored in foo.

How to remove duplicates from Python list?

In addition, we can say itertools groupby is one of the best methods to remove Duplicates from the Python list. As it uses the builtin module, so by default it’s speed will be higher than the above-mentioned methods to remove duplication in Python list.

How do I remove duplicates from an array of elements?

Just remove the iteration part. Also, an easy way to remove duplicates, if you are not interested in the order of the elements is to use set. So, in your case you can initialize lst as - lst=set () . And then use lst.add () element, you would not even need to do a check whether the element already exists or not.

How do you remove duplicates from a list in latex?

A List with Duplicates. mylist = ["a", "b", "a", "c", "c"] mylist = list (dict.fromkeys (mylist)) print(mylist) Create a dictionary, using the List items as keys. This will automatically remove any duplicates because dictionaries cannot have duplicate keys.

Are We removing all duplicated elements in the project?

This is what we want to achieve. So we are not removing all duplicated elements. We are only eliminating consecutive duplicate elements. Let’s check out the 3 different ways.


3 Answers

Convert to a set:

a = set(a)

Or optionally back to a list:

a = list(set(a))

Note that this doesn't preserve order. If you want to preserve order:

seen = set()
result = []
for item in a:
    if item not in seen:
        seen.add(item)
        result.append(item)

See it working online: ideone

like image 181
Mark Byers Avatar answered Oct 27 '22 11:10

Mark Byers


Use the set type to remove duplicates

a = list(set(a))
like image 13
Krumelur Avatar answered Oct 27 '22 13:10

Krumelur


def getUniqueItems(iterable):
result = []
for item in iterable:
    if item not in result:
        result.append(item)
return result

print (''.join(getUniqueItems(list('apple'))))

P.S. Same thing like one of the answers here but a little change, set is not really required !

like image 6
murasing Avatar answered Oct 27 '22 11:10

murasing