Program to remove duplicate elements from the list in python

Removing duplicate element form the list in python is easy unlike c program.Python includes datatype for sets.A set is an unordered collection of elements without duplicate entries.We can just make set of the list and again make list of that set to get the output.

Program I :

inputlist = [22,33,33,44,55,55,55,66,66]
outputlist = list(set(inputlist))
print outputlist

Just three lines of code and you will get the list without any duplicate elements.There are several other technique also for writing program to find the distinct element in the list without using set.

Program II :

inputlist = [22,33,33,44,55,55,55,66,66]
outputlist = []
for element in inputlist:
         if element not in outputlist:
            outputlist.append(element)
print outputlist

You can use http://www.codeskulptor.org/ to run the python programs online.

Comments

Popular posts from this blog

Tricky Questions or Puzzles in C

Program to uncompress a string ie a2b3c4 to aabbbcccc

Number series arrangement puzzles