Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python List Iteration into One-Liner [closed]

Tags:

python

list

I'm currently learning Python.

How do I put this:

dates = list()  
for entry in some_list:  
    entry_split = entry.split()    
    if len(entry_split) >= 3:  
        date = entry_split[1]
        if date not in dates:  
            dates.append(date)

into a one-liner in Python?

like image 829
dfernan Avatar asked Dec 20 '22 04:12

dfernan


1 Answers

Instead of a 1-liner, probably it's easier to understand with a 3-liner.

table = (entry.split() for entry in some_list)
raw_dates = (row[1] for row in table if len(row) >= 3)
# Uniquify while keeping order. http://stackoverflow.com/a/17016257
dates = list(collections.OrderedDict.fromkeys(raw_dates))
like image 163
kennytm Avatar answered Dec 30 '22 09:12

kennytm