Skip to content Skip to sidebar Skip to footer

Print An Element In A List Based On A Condition

I have a list of lists which contain a first name, last name, and points scored. list1 = [['david', 'carter', 6], ['chris', 'jenkins', 0], ['john', 'wells', 5], ['ryan', 'love', 0]

Solution 1:

>>>[item for item in list1 if item[2]==0]

ans:-
[['chris', 'jenkins', 0], ['ryan', 'love', 0]]

Solution 2:

Keep it simple:

for e in list1:
    if e[2] == 0:
        print e

Solution 3:

>>> from itertools import ifilterfalse
>>> from operator import itemgetter
>>> list(ifilterfalse(itemgetter(2), list1))
[['chris', 'jenkins', 0], ['ryan', 'love', 0]]

Post a Comment for "Print An Element In A List Based On A Condition"