# Helicopter Prison escape - fundamental query related to nested for loop

My Code:

``````for x in data:
for z in attempts_per_year:
if z[0]== x[0]:

z[1]+=1

print(attempts_per_year)
``````

What I expected to happen:
I got the exact solution I was looking for . However my query is that this nested for loop worked since the years mentioned in the wiki data was arranged in ascending order - example 1971,1973,1978,1981,1981 etc .
What if the data wasn’t sorted in ascending order example - 1973,1971,1981,1971,1971,1981. This loop would not have had the same output i presume in such a case . Please tell me what could be the solution

What actually happened:

``````There was no error , the solution was exactly as i had expected
``````

The output could be the same but in an unsorted manner.
When looping, we are iterating one element after the other.
`data` is al list of list, ie, the elements are lists in this list. The first element in this element (list) is the year.
`attempts_per_year` is a is also a list of list, where the first element of the lists is the year.

When looping data, we have a list as the element, for this list we check the first element (`year`) if it is similar to the first element also `year` when looping attempts per year

Hey Victor . Let’s take an example. Suppose the years in data is - 1973,1971,1973,1971,1971 ( arranged the same way ) . Let the years in attempts_per_year be arranged in ascending order , i.e 1971,1973,1978 . Now I write the below code and execute it

for x in data:
for z in attempts_per_year:
if z[0]== x[0]:

``````        z[1]+=1
``````

print(attempts_per_year)

The 1st iteration would check take 1973 as x[0] and compare with 1971 as z[0] . The output would be 1971 , 0

Question 1) Would the 2nd iteration check take x[0] as 1971 and compare with z[0] as 1971, and print the output as - 1971 , 0 ?
Question 2) Would would be the 3rd and 4th iteration and output ?