0

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?

Vesper
  • 795
  • 1
  • 9
  • 21
  • 1
    what about the condition? that if the state is present in the connection then remove it from the connection and then print it in the dictionary. – Vesper Mar 10 '21 at 12:58
  • 1
    your `summary` dictionary is not valid since dictionary can only have unique keys but you have `State` and `connection` as keys multiple times. – Krishna Chaurasia Mar 10 '21 at 12:59
  • Yeah I have updated the expected output – Vesper Mar 10 '21 at 13:02
  • But really? We can't have multiple keys with the same name? I am just asking. – Vesper Mar 10 '21 at 13:03
  • 1
    it is just how dictionary specification is defined, it is basically a key, value pair mapping. btw your expected object is still not clear, you can't have `summary` object to represent two dictionaries, may be you need a list of dictionaries. – Krishna Chaurasia Mar 10 '21 at 13:05
  • and why does `Delhi` not have `Rajasthan` in the connection list? please formulate the problem description clearly – Krishna Chaurasia Mar 10 '21 at 13:06
  • Becoz that's how it should be. I mean I can't include any of the items which are present in the state list. – Vesper Mar 10 '21 at 13:08

1 Answers1

1
connection = ['Rajasthan',12,'Punjab',234,'Haryana',333,'Himchal Pradesh', 320]
states = ['Delhi', 'Rajasthan']

def f(c, s):
    c = iter(c)
    for conn in c:
        if conn in s:
            next(c)
            continue
        yield conn

list(f(connection, states))
['Punjab', 234, 'Haryana', 333, 'Himchal Pradesh', 320]

This uses an iterator to loop over the connection list and to skip the number following a "match" (where the value is in the states list).

An iterator always implements a method Iterator.__next__() that can be called with next(Iterator), to get the next value.

So when there is a match if conn in s, we call next(c) to get the following number from c (that we don't care about). And then with continue the loop continues with the next state name.

If the value is not in s, it is yielded. Here is a great answer about generator functions.

C14L
  • 12,153
  • 4
  • 39
  • 52
  • Hey, thanks, man this would work, I actually don't care if the result is in the list or dictionary. But can you please a little bit why exactly this works? – Vesper Mar 10 '21 at 13:13
  • 1
    I added some explanation. Feel free to ask if its still unclear. – C14L Mar 10 '21 at 14:03