Skip to content

Instantly share code, notes, and snippets.

@mysticBliss
Last active February 2, 2018 10:44
Show Gist options
  • Save mysticBliss/8ec0d7379be839853b15592b3ce59275 to your computer and use it in GitHub Desktop.
Save mysticBliss/8ec0d7379be839853b15592b3ce59275 to your computer and use it in GitHub Desktop.
Getting unique elements from List

Getting unique elements from List

mylist = [1,2,3,4,5,6,6,7,7,8,8,9,9,10]

Using Simple Logic from Sets - Sets are unique list of items

mylist=list(set(mylist))

In [0]: mylist
Out[0]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Using Simple Logic

newList=[]
for i in mylist:
    if i not in newList:
        newList.append(i)

In [0]: mylist
Out[0]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Using pop method

pop removes the last or indexed item and displays that to user. REFERENCE

k=0
while k < len(mylist):
    if mylist[k] in mylist[k+1:]:
        mylist.pop(mylist[k])
    else:
        k=k+1

In [0]: mylist
Out[0]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Using Numpy

import numpy as np
np.unique(mylist)

In [0]: mylist
Out[0]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment