I two lists: one is the state list and the other one is the connections list:
connection = ['Rajasthan',12,'Punjab',234,'Haryana',333,'Himchal Pradesh', 320]
states = ['Delhi', 'Rajasthan']
I want to create a dictionary with each state name as the index and if that state name occurs in the connection
list then that should be removed and also the number alongside with them, so if Rajastjan
is occurring in the connection
list then it should be removed and also the numbers which present alongside with it 12
So summary should look like this:
summary = {
'State': Delhi,
'Connection: ['Punjab',234,'Haryana',333,'Himchal Pradesh', 320],
}
summary ={
'State': Rajasthan,
'Connection: ['Punjab',234,'Haryana',333,'Himchal Pradesh', 320],
}
My approach for this is:
connection = ['Rajasthan',12,'Punjab',234,'Haryana',333,'Himchal Pradesh', 320]
states = ['Delhi', 'Rajasthan']
summary = {}
for i in range(len(states)):
if states[i] in connection:
connection.remove(states[i])
summary = {
'name': states[i],
'connection': connection
}
if states[i+1] in connection:
connection.remove(states[i+1])
summary = {
'name': states[i+1],
'connection': connection
}
print(summary)
and yes I know it is giving me the outOfIndex
error. Can anyone help me?