Python Find Tuple by Geographic Priority Requirement:

Python Function will read a LIST of TUPLES. The structure of the tuple is (&quot;STRING&quot;, &quot;STRING&quot;,&quot;STRING&quot;,INT,&quot;GEOGRAPHIC VALUE&quot;)
This Geographic value can have 5 values &quot;zip&quot;, &quot;city&quot;, &quot;county&quot;, &quot;state&quot; and &quot;country&quot;
Now if a Tuple has elements where geographic values are (....,&quot;zip&quot;),(....,&quot;county&quot;), (....,&quot;country&quot;) then function should return the element/tuple which contains &quot;zip&quot;
Examples :
Input : [('aa', 'bb', 'cc', 1, 'state'), ('aa', 'bb', 'cc', 2, 'zip')]
Output : ['aa', 'bb', 'cc', 2, 'zip']
In other words 'zip' is higher priority(hp) than 'city'.
'city' is HP than 'county',
'county' is HP than 'state'
and 'state' is HP than 'country'.

I have written code for that but being new to Python, the code looks clunky. Is there any shorter method?
def unique_list(input):
    my_list = input
    print(my_list)
    last_list = []
    zipcnt = -1
    citicnt = -1
    countycnt = -1
    statecnt = -1
    return_list_zip = []
    return_list_city = []
    return_list_county = []
    return_list_state = []
    return_list_country = []
    for j in range(len(my_list)):
          if(my_list[j][4]) == &quot;zip&quot;:
             zipcnt = len(my_list)
             return_list_zip = list(my_list[j])
             continue
          elif (my_list[j][4] == 'city' and zipcnt == -1):
              citycnt = len(my_list)
              return_list_city = list(my_list[j])
              continue
              
          elif (my_list[j][4] == 'county' and zipcnt == -1 and citicnt == -1):
              countycnt = len(my_list)
              return_list_county = list(my_list[j])
              continue
          elif (my_list[j][4]  == 'state'and zipcnt == -1 and citicnt == -1 and countycnt == -1):
              statecnt = len(my_list)
              return_list_state = list(my_list[j])
              continue
          elif (my_list[j][4] == 'country'and zipcnt == -1 and citicnt == -1 and countycnt == -1 and statecnt == -1):
              return_list_country = list(my_list[j])
              continue
    if(zipcnt != -1):
        return_list = return_list_zip
    elif(citycnt != -1):
        return_list = return_list_city
    elif(countycnt != -1):
        return_list = return_list_county
    elif (statecnt != -1):
        return_list = return_list_state
    else:
        return_list = return_list_country
    return return_list
input_list = [ (&quot;aa&quot;, &quot;bb&quot;, &quot;cc&quot;, 1,&quot;state&quot;), (&quot;aa&quot;,&quot;bb&quot;,&quot;cc&quot;,2,&quot;zip&quot;)]
input_list1 = [(&quot;XX&quot;,&quot;YY&quot;,&quot;NN&quot;,1,&quot;country&quot;),(&quot;XX&quot;,&quot;YY&quot;,&quot;NN&quot;,2,&quot;city&quot;),  (&quot;XX&quot;,&quot;YY&quot;,&quot;NN&quot;,3,&quot;state&quot;)]
input_list2 = [(&quot;NN&quot;,&quot;FF&quot;,&quot;PP&quot;,10,&quot;state&quot;),(&quot;NN&quot;,&quot;FF&quot;,&quot;PP&quot;,11,&quot;country&quot;),(&quot;NN&quot;,&quot;FF&quot;,&quot;PP&quot;,10,&quot;city&quot;)]
print(unique_list(input_list))
print(unique_list(input_list1))
print(unique_list(input_list2))
You're right, it is clunky.

The first important step is to pick a function name that succinctly describes the task that it performs, namely, picking the most specific address component.

Once you do that, you might realize that "the most X of Y" means that you should use the max() function.  You just have to map each tuple to some quantitative measure of "specificity".

def most_specific_address(addresses):
    SPECIFICITY = {
        'country': 1, 'state': 2, 'county': 3, 'city': 4, 'zip': 5,
    }
    return max(addresses, key=lambda addr: SPECIFICITY[addr[4]])