I am trying to create a new list by comparing the list items to dictionary keys, if the match is found then i append a value of dictionary to new list that was globally initialized. the code i am trying is as below
dict_1 = {'OMSS': '10.1.1.0/24', 'A&A': '10.1.2.0/24', 'AFM': '10.1.3.0/24', 'ATM': '10.1.4.0/24'}
list_1 = ['A&A', 'A&A', 'OMSS', 'OMSS', 'A&A', 'AFM', 'A&A', 'AFM', 'A&A']
list_2 = ['OMSS ', 'OMSS ', 'A&A ', 'A&A ', 'AFM ', 'A&A ', 'AFM ', 'A&A ', 'AFM ']
list_of_list1 = []
for s1 in list_1:
#print (s2)
for key, value in dict_1.items():
#print (key)
if s1 == key:
list_of_list1.append(value)
print (list_of_list1)
list_of_list2 = []
for s2 in list_2:
#print (s2)
for key, value in dict_1.items():
#print (key)
if s2 == key:
list_of_list2.append(value)
print (list_of_list2)
when i run this i get below output
['10.1.2.0/24', '10.1.2.0/24', '10.1.1.0/24', '10.1.1.0/24', '10.1.2.0/24', '10.1.3.0/24', '10.1.2.0/24', '10.1.3.0/24', '10.1.2.0/24']
{'OMSS': '10.1.1.0/24', 'A&A': '10.1.2.0/24', 'AFM': '10.1.3.0/24', 'ATM': '10.1.4.0/24'}
[]
i am trying to figure out why "list_of_list2" is coming out as empty?
list_1andlist_2?if s2.strip() == key:(although there are simpler ways to do what you're doing, this would fix your code)