com
com

Reputation: 79

Python comparing individual lists of lists elements

I'm trying to compare elements of lists of lists to find which values are larger/equal to each other.

x = [[8, 12.5], [1.5, 12.6], [35, 137], [3.8, 145], [48, 1.8], [15, 67]]
y = [[0, 14], [6.4, 224], [8.5, 123], [6.5, 26.1], [4.1, 57], [58, 61]]

if x[i,0] > y[i,0]:
   #do this
elif x[i,0] < y[i,0]:
   #do that
elif x[i,0] == y[i,0]:
   #do other 

When I try to compare the elements, I get an error message that says:

TypeError: list indices must be integers, not tuple

Is there a way to compare tuple/list elements? I see all kinds of posts that find common elements or comparing whole lists, but not comparing elements. I'm using python 2.7.3

Upvotes: 0

Views: 140

Answers (1)

Blender
Blender

Reputation: 298106

Almost. You get a list with the first index and then you can get an element out of that list with the second:

if x[i][0] > y[i][0]:

Upvotes: 4

Related Questions