Skip to content
Advertisement

Problem with comparing two items in a list of lists

I have this list

JavaScript

what I am trying to do is compare each item within the lists to each other (within the same list), to find out what the minimum is. In the first case, it would be 3.0, in the second, it would be 3.1.

Now what I am trying to do is, if the minimum is the first element, assign that element (3.0) to a dictionary key named M1, and if the minimum is the second element, assign that element to a dictionary key named M2.

This is what I’ve come up with:

JavaScript

But it gives me this error: TypeError: tuple indices must be integers or slices, not float

What do I do?

Advertisement

Answer

You don’t need the inner loop. jk is the tuple, you just need to compare jk[0] with jk[1]. You can simplify this by unpacking to two variables when looping.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement