nested list filtering in python? -


is possible create nested filtering list? have list of bookmarks contains list of tags. want filter bookmarks specific tag.

def filterlistbytag(bookmarklist, tag):     filteredlist = []     #filter list single tag     b in bookmarklist:         t in b[1]:             if t == tag:                 filteredlist.append (b)     return filteredlist 

i have coded nested loop achive there pythonic way using filter or [] below ? :-)

 my_list = [i in my_list if i.attribute == value]  filter(lambda x: x.attribute == value, my_list) 

e.g.: bookmarks tag "newcar-7-seats"

example of input :

print mybookmarks.bookmarks[0] (u'http://qt-project.org/doc/qt-4.8/examples-itemviews.html', [u'python'], u'item views examples | documentation | qt project', u'', datetime.datetime(2013, 7, 10, 13, 38, 9))  print mybookmarks.bookmarks[1] (u'http://qt-project.org/doc/qt-4.8/model-view-programming.html', [u'python'], u'model/view programming | documentation | qt project', u'', datetime.datetime(2013, 7, 10, 13, 36, 23))  print mybookmarks.bookmarks[4] (u'http://www.gebrauchtwagen.at/', [u'newcar-7-seats'], u'gebrauchtwagen.at \u2013 auto, autos, jahreswagen, neuwagen, oldtimer, unfallwagen, automarkt, autob\xf6rse', u'', datetime.datetime(2013, 7, 9, 8, 37, 35))  print mybookmarks.bookmarks[5] (u'http://www.car4you.at/gebrauchtwagen', [u'newcar-7-seats'], u'car4you | gebrauchtwagen, autos, fahrzeuge und motorr\xe4der kaufen und verkaufen', u'', datetime.datetime(2013, 7, 9, 8, 37, 25)) 

filtered list on

print mybookmarks.bookmarks[4]  (u'http://www.gebrauchtwagen.at/', [u'newcar-7-seats'], u'gebrauchtwagen.at \u2013 auto, autos, jahreswagen, neuwagen, oldtimer, unfallwagen, automarkt, autob\xf6rse', u'', datetime.datetime(2013, 7, 9, 8, 37, 35))  print mybookmarks.bookmarks[5] (u'http://www.car4you.at/gebrauchtwagen', [u'newcar-7-seats'], u'car4you | gebrauchtwagen, autos, fahrzeuge und motorr\xe4der kaufen und verkaufen', u'', datetime.datetime(2013, 7, 9, 8, 37, 25)) 

this should equivalent,

filteredlist = [b b in bookmarklist if tag in b[1]] 

i think meant break after append otherwise you'd multiples.


Comments