CODE:
  1. list = ['one', 'two', 'three', 'one', 'one', 'four', 'two']
  2. #convert the list into a set.  An element can only exist once within a set
  3. set = set(list)
  4. #convert the set back into a list type
  5. list = list(set)
  6. print list

Are those variable names confusing? Let's look at that example again:

CODE:
  1. fruits = ['apple', 'bananas', 'cantaloupes', 'apple', 'apple', 'durian', 'bananas']
  2. #convert the list into a set.  An element can only exist once within a set
  3. fruits_unique = set(fruits)
  4. #convert the set back into a list type
  5. fruits = list(fruits_unique)
  6. print fruits

Share
Tagged with:  
Share →

One Response to Python: removing repeated values in a list

  1. BooL says:

    Nice way!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>