View Full Version : [PYTHON] Remove duplicate from array

11 November 2010, 04:08 PM

I'm pretty new to python so sorry if this question looks too obvious for some of you...
I have an array with duplicate entries that I need to remove.
For example
array = ['blue','red','red','green']
needs to be
array = ['blue','red','green']
Is there any python function to do so ? Or do I have to loop inside the array ..
Thx for your help


11 November 2010, 04:29 PM
You can use the python sets module. Those are essentially unordered collections that enforce uniqueness among its members.


# DISCLAIMER: Not actually tested code
import sets
array = ['blue','red','red','green']
uniquearray = sets.Set(array)

Should result in what you want. For more info:

11 November 2010, 04:42 PM
sets are builtin now Keilun...

array = list(set(array))However, this is a very interesting blog post on the whole subject:


11 November 2010, 04:45 PM
Ah right. I'm on an old version of python for my current project. :) That link is news to me! Nice find. Thanks!

11 November 2010, 09:53 AM
Thank you both for your answers, intersting reading.
@NaughtyNathan : you add the list function to reconvert the array from set to list ?
Thx again


11 November 2010, 10:16 AM
Yes Lc, that's exactly right. of course you can omit this step, but you started with a list, and if you aren't constructing or adding anything else onto it it makes sense to end up with a list as you can do a lot more stuff to/with a list than you can to a set.


11 November 2010, 05:27 PM
hehe @nathan: I thought of the same posting! So you actually got it! You didn't reply when I sent you that.:thumbsup:

So which function do you prefer now?

01 January 2011, 09:36 PM
how to do with mel for int/float array ?

01 January 2011, 01:20 AM
don't double post

01 January 2011, 04:25 PM
You can also use a dictionary:
listOfLights = ['keyLight', 'rimLight', 'backLight', 'fillLight', 'rimLight']
listOfLights = dict.fromkeys(listOfLights) #Convert to a dictionary
listOfLights # Now a dictionary
listOfLights = listOfLights.keys() # Convert back to a list

CGTalk Moderation
01 January 2011, 04:25 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.