johnmahugu

python - getting rid of duplicate items in a list

Jun 14th, 2015
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.32 KB | None | 0 0
  1. Getting rid of duplicate items in a list
  2.  
  3. The trick is to temporarly convert the list in into a dictionnary:
  4. >>> mylist = [3,5,8,5,3,12]
  5. >>> print dict().fromkeys(mylist).keys()
  6. [8, 3, 12, 5]
  7. >>>
  8.  
  9. Since Python 2.5, you can also use sets:
  10. >>> mylist = [3,5,8,5,3,12]
  11. >>> print list(set(mylist))
  12. [8, 3, 12, 5]
  13. >>>
Advertisement
Add Comment
Please, Sign In to add comment