2

So my code is

for i in range(len(temp1)-1):
   sim = similar(temp1[i][0],temp1[i+1][0])
   #and a few more stuff here

It is important that in every loop I compare two adjacent items from the list.

How can I write this part with direct access to list's items while avoiding nested loops?

I want something like

for item in temp1:
   sim = similar(item[0], next_item[0])

Thanks

costisst
  • 381
  • 2
  • 6
  • 22
  • I'm not sure if the duplicate is appropriate (but I think so). Please leave me a comment if it's not what you asked for. – MSeifert Jun 01 '17 at 12:36
  • @MSeifert I guess I'm looking for what is mentioned in the second answer as 'pairwise combinations' but I cannot understand how they are created using itertools functions. The accepted answer is what I have done and I want to avoid. – costisst Jun 01 '17 at 12:46
  • 1
    Maybe the third answer is what you're looking for `for item1, item2 in zip(temp1, temp1[1:]): sim = similar(item1, item2)`? – MSeifert Jun 01 '17 at 12:50
  • @MSeifert I guess that one should do the job, thanks :) – costisst Jun 01 '17 at 12:57

0 Answers0