Hello! Explain, please, in detail the meaning of the index () method in the list. Not just an example, but with an explanation. There is code that works but understand how the index () method works in this code does not reach ...

test=[ {'is_blocked': 0, 'ip': '10.10.10.1', 'id': 113}, {'is_blocked': 0, 'ip': '10.10.10.16', 'id': 113}, {'is_blocked': 0, 'ip': '10.10.10.1', 'id': 114}, {'is_blocked': 0, 'ip': '10.10.10.16', 'id': 115}, {'is_blocked': 0, 'ip': '10.10.10.1', 'id': 114}, {'is_blocked': 0, 'ip': '10.10.10.16', 'id': 116} ] def f(arr): vals, ips, res=[], [], [] for i in arr: ip= i['ip'] del i['ip'] try: ips[vals.index(i)]+= ","+ip except: vals.append(i) ips.append(ip) for i in range(len(vals)): vals[i]['ip']=ips[i] res.append(vals[i]) print res 

    1 answer 1

    index - shows (ATTENTION!) index, that is, the number of the requested item in the list, the numbering starts from zero.

     >>> list = ['zero','one','two'] >>> list.index('zero') 0 >>> list.index('one') 1 >>> list.index('two') 2 

    UPD

     ips[vals.index(i)]+= ","+ip vals.index(i) // вызывает ошибку если элемента нету в списке //и срабатывает код except: vals.append(i) ips.append(ip) // в противном случае берется ip и добавляется к элементу который находится под этим индексом