dimanche 29 mars 2015

Python 3.4: adding value to list if condition exists


Vote count:

0




i have a scenario like this one:



mainList = [[9,5],[17,3],[23,1],[9,2]]
secondaryList = [9,12,28,23,1,6,95]
myNewList = []

myNewList.append([[a,b] for a,b in mainList if a in secondaryList])


this, return me to me:



myNewList = [[9,5],[23,1],[9,2]]


but I need only the first occourance of "a". In other words I need to obtain:



myNewList = [[9,5],[23,1]]


How can I achieve this?



asked 36 secs ago

jul

62






Python 3.4: adding value to list if condition exists

Aucun commentaire:

Enregistrer un commentaire