There is a list

[[-0.56, 0.08], [-0.54, 0.09], [-0.52, 0.1], [-0.5, 0.11]] 

All values ​​in it float

There is a function

 def Area(list1): s = 0 for i in list1: s += (list1[i][0])*(list1[i+1][1]) - (list1[i+1][0])*(list1[i][1]) return s 

But she does not want to work with sub-lists at all, gives out

 TypeError: list indices must be integers, not list 

How to solve or get around? Moreover, this problem is observed only during the cycle.

    5 answers 5

    In the for i in list1: ... loop for i in list1: ... the i parameter takes values ​​from sublists, not indexes. You only need to change the cycle to this:

     for i in range(len(list1)-1): ... 

    Of course, iteration with indices is usually done through enumerate(list1) , but I still can’t figure out how to enumerate through the last but one element (without list sections or other even more perverted methods).

    • Sorry I did not notice your comment before, I would not need to think of it myself. Thanks for the answer, anyway (:: user208919

    Try the following solution:

     list = [[-0.56, 0.08], [-0.54, 0.09], [-0.52, 0.1], [-0.5, 0.11]] def area(list): s = 0 i = 0 while i < len(list) - 1: s += (list[i][0])*(list[i+1][1]) - (list[i+1][0])*(list[i][1]) i += 1 return s 

    In your case, i is an element of the list1 list - also a list. In my proposed solution, i is an index, a number.

    • "It seems to me that the problem is that i takes values ​​from 0 to len (list)" - you are mistaken. If you try to print for i in list1: print(i) , then you will see that the sublists are displayed, not the indices. This is indicated by the error indicated in the question ("indices should be integers, not lists"). The inability to read at index i+1 for the last element of the list is another error that the questioner has not yet encountered at this stage. - insolor
    • You are right, thank you. I edit the answer. - y_v
    • @yv Yes, I have not reached the error with the impossibility of getting out of the list limit. Referring to your decision, I solved the problem like this: for i in range(len(list_of_koordinates)-1) I completely forgot something like this - user208919
     def add(_list): lst = _list[0] for a in _list[1:]: yield lst[0] * a[1] - a[0] * lst[1] lst = a print(sum(add([[-0.56, 0.08], [-0.54, 0.09], [-0.52, 0.1], [-0.5, 0.11]]))) 
    • Try to write more detailed answers. I am sure the author of the question would be extremely grateful for your expert commentary on the code. - Nicolas Chabanovsky
    • IMHO, overinserving for such a simple task. - insolor

    The idiomatic way to organize such a cycle in Python does not use calls on indices, but works directly with elements:

     >>> points = [[-0.56, 0.08], [-0.54, 0.09], [-0.52, 0.1], [-0.5, 0.11]] >>> sum(x1*y2 - x2*y1 for (x1,y1), (x2,y2) in zip(points, points[1:])) -0.021600000000000001 
       l = [[-0.56, 0.08], [-0.54, 0.09], [-0.52, 0.1], [-0.5, 0.11]] print(sum(l[q][0] * l[q+1][1] - l[q+1][0] * l[q][1] for q in range(len(l)-1)))