Search code examples
pythonfor-loopsetunique-values

How to get multiple values from a for loop?


I have a list of records from a column, the list is named as dates. I am trying to get different dates out of the list. The list have many repetitive dates, such as 1/1/2010,1/1/2010, …. but there are different dates too. But if i use:

for date in dates: ....

it's repeating the loop for every single date(no matter if it is the same or not), not different dates. How could I tell it to do:

for differentdate in dates:... 

The language is Python!!


Solution

  • for date in set(dates):
    

    set() makes a collection out of the unique elements in another collection. Note: this may not preserve the order of the original list, so if you require that order to be preserved, go with @GregHewgill's answer.