Finding the first index in a list greater than a particular value

MRAB python at mrabarnett.plus.com
Sun Aug 14 16:23:12 EDT 2016


On 2016-08-14 19:40, Atri Mahapatra wrote:
> On Monday, 15 August 2016 00:03:59 UTC+5:30, MRAB  wrote:
>> On 2016-08-14 19:17, Atri Mahapatra wrote:
>> > I have a list of dictionaries which look like this:
>> > [{'Width': 100, 'Length': 20.0, 'Object': 'Object1'}, {'Width': 12.0, 'Length': 40.0, 'Object': 'Object2'}...... so on till 10]
>> >
>> > I would like to find the first index in the list of dictionaries whose length is greater than a particular value
>> >
>> > f=lambda seq, m: [ii for ii in range(0, len(seq)) if seq[ii]['Length'] > m][0] and it throws an error
>> >
>> > can  anyone suggest a way  to do it?
>> >
>> What is the error?
>
> Traceback (most recent call last):
>   File "<pyshell#12>", line 1, in <module>
>     f(lengthlist, 10)
>    f=lambda seq, m: [ii for ii in range(0, len(seq)) if seq[ii] > m][0]
> TypeError: unorderable types: dict() > int()
>
That's not the same code as your original post.

Your original post has:

     seq[ii]['Length'] > m

which is correct, but your traceback shows:

     seq[ii] > m

which tries to compare the entire dict to m, hence the exception.




More information about the Python-list mailing list