Skip to content
Advertisement

Compare each element of a list in existing order with elements of a second list in order as long as items in lists are equal

Compare each element of a list in existing order with elements of a second list in existing order as long as items in lists are equal. Stop if they’re not equal and give me as result the index and name of the last match.

I thought it’s straightforward with a while loop but it seems like this has to be approached with a for-loop.

My List one I want to compare: nk_script_file_path

JavaScript

My second list I want to compare it to: node_filepath

JavaScript

What I’ve tried

JavaScript

Result I’m looking for:

JavaScript

or

JavaScript

Advertisement

Answer

You can use zip() with enumerate() to find first index where’s difference. In this example if no difference is found, value of i is equal to -1:

JavaScript

Prints:

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