Taimur Taimur - 10 months ago 47
Python Question

How to compare values within an array in Python - find out whether 2 values are the same

I basically have an array of 50 integers, and I need to find out whether any of the 50 integers are equal, and if they are, I need to carry out an action.

How would I go about doing this? As far as I know there isn't currently a function in Python that does this is there?

Answer Source

If you mean you have a list and you want to know if there are any duplicate values, then make a set from the list and see if it's shorter than the list:

if len(set(my_list)) < len(my_list):
    print "There's a dupe!"

This won't tell you what the duplicate value is, though.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download