Python: Make my code efficient and less time complexity, My Code is about fetch details from Excel using ‘openpyxl’

I have this code so far:

Dictionary: which has Excel Data

DATA_DICT = {   '1238': {     'ipAddrPrim': ['IP1', 'IP2', 'IP3'],     'mmeName': ['N1', None, 'N3'],     's1LinkStatus': ['available', 'unavailable', 'available']   },   '1236': {     'ipAddrPrim': ['IP1', 'IP4'],     'mmeName': ['N4', 'N5'],     's1LinkStatus': ['available', 'available']   },   '1238': {     'ipAddrPrim': ['IP3', 'IP4', 'IP5'],     'mmeName': ['N7', 'N9', None],     's1LinkStatus': ['available', 'available', 'unavailable']   },   .... } 

My Code: which it’ll give the source and target lists

def finding_distinct_ips():     result_list, source_list, target_list, src_mrbts_key = [], [], [], []     unique_mrbts_list = remove_duplicates(MRBTS) #MRBTS has all MRBTS ids [1234,1234,1234,1236,1236,1238,1238,1238]                                                  #remove_duplicates(MRBTS) -> [1234,1236,1238]     for mrbts_id in unique_mrbts_list:         rest = {k: v for k, v in DATA_DICT.items() if k != mrbts_id}         main_key_ip_list = DATA_DICT[mrbts_id].get('ipAddrPrim')         status = DATA_DICT[mrbts_id].get('s1LinkStatus')         mme_name = DATA_DICT[mrbts_id].get('mmeName')         for key, val in rest.items():             rest_key_ip_list = val['ipAddrPrim']             result = compare(main_key_ip_list, rest_key_ip_list) #--> lst3 = list(set(main_key_ip_list) - set(rest_key_ip_list))             for ip_addr in result:                 ip_row_index = main_key_ip_list.index(ip_addr)                 if ip_addr != [] and status[ip_row_index] != 'unavailable':                     mme_at_ip = mme_name[ip_row_index]                     result_list = (mrbts_id, key, mme_at_ip, ip_addr)                     if result_list[0:2][::-1] not in src_mrbts_key:                         src_mrbts_key.append(result_list[0:2])                         source_list.append(result_list)                     else:                         target_list.append(result_list)     return source_list, target_list 

Explanation:

I have an Excel spreadsheet:

MRBTS  IPs  NAMES STATUS 1234   IP1   N1    Yes 1234   IP2   -     No 1234   IP3   N3    Yes 1236   IP1   N4    Yes 1236   IP4   N5    Yes 1238   IP3   N7    Yes 1238   IP4   N9    Yes 1238   IP5   -     No 

My Code will full fill these requirements:

1) I need to compare each MRBTS with each other just like

comp(1234,1236),comp(1234,1238),comp(1236,1234),comp(1236,1238),comp(1238,1234),comp(1238,1236),...] but not with same mrbts like comp(1234,1234).

2) Lets take (if status=Yes) for comp(1236,1238): In 1236's I have some list(IPs[IP1,IP4] and Ns[N4,N5]) so again i need to compare those list with 1238's list(IP[IP3,IP4] and Ns[N7,N9]) {Here we didn’t considered IP5 as status = No}, if any IP in 1236(IP list) is not found in 1238(IP list) then return that IP,Name and only if status=Yes

compare(1236,1238) = [1236,IP1,N4] and compare(1238,1236) = [1238,IP3,N7] 

3) Now do the comparison for all combis and get results and store like source = [compare(1236,1238)] and target = [compare(1238,1236)] Note: Here we can observe source has right ordered comparison and target has reverse ordered comparison.

Now I want, help to make above code efficient and less the time complexity:

Note:

i. Avoid Nested for loops as my Original excel has a huge number of data it’ll take time to read, else if u use then make execution fast.

ii. Use openpyxl or pyxlsb packages only