bob_cobb bob_cobb - 3 months ago 6
Python Question

Plucking an inner most item by its index with list comprehension

I'm trying to get better at understanding list comprehension, and I started this thing with a list of tuples that I've cast to a list of lists.

With list comprehension, how can I pluck out from the inner most list's index that contains a

2
in the last index?

results = [
[
[529L, u'wat', u'wat', 3L, 2], [530L, u'wat', u'wat', 3L, 1], [531L, u'wat', u'wat', 3L, 1]
], [
[533L, u'weeeee', u'weeeee', 3L, 1], [534L, u'weeeee', u'weeeee', 3L, 1]
]
]


Would be:

results = [
[
[530L, u'wat', u'wat', 3L, 1], [531L, u'wat', u'wat', 3L, 1]
], [
[533L, u'weeeee', u'weeeee', 3L, 1], [534L, u'weeeee', u'weeeee', 3L, 1]
]
]

Answer
results = [[x for x in lst if x[-1]!=2] for lst in results]