Home > Blockchain >  how to remove item from list with children
how to remove item from list with children

Time:03-07

I am currently working on python and I am relatively new to this so I have a list 'D'

D = {
            'Dunwich': ['Blaxhall', 'Harwich'],
            'Blaxhall': ['Dunwich', 'Harwich', 'Feering'],
            'Harwich': ['Dunwich', 'Blaxhall', 'Tiptree', 'Claston'],
            'Tiptree': ['Feering', 'Harwich', 'Claston', 'Maldon'],
            'Feering': ['Blaxhall', 'Tiptree', 'Maldon'],
            'Maldon': ['Feering', 'Tiptree', 'Claston'],
            'Claston': ['Maldon', 'Tiptree', 'Harwich']

        }

And another list 'Result'

Result= ['Dunwich', 'Harwich', 'Claston', 'Maldon']

What I want is to pop from list D where key of D and item of Result is same for example if the Result is as mentioned above then after comparison and popping list D should be

D = {
            'Dunwich': ['Blaxhall', 'Harwich'],
            'Harwich': ['Dunwich', 'Blaxhall', 'Tiptree', 'Claston'],
            'Maldon': ['Feering', 'Tiptree', 'Claston'],
            'Claston': ['Maldon', 'Tiptree', 'Harwich']

        }

I hope this query was understandable, kindly correct me if I am wrong somewhere and please help me solve this

CodePudding user response:

You can use dict comprehension:

D = {
        'Dunwich':  ['Blaxhall', 'Harwich'],
        'Blaxhall': ['Dunwich', 'Harwich', 'Feering'],
        'Harwich':  ['Dunwich', 'Blaxhall', 'Tiptree', 'Claston'],
        'Tiptree':  ['Feering', 'Harwich', 'Claston', 'Maldon'],
        'Feering':  ['Blaxhall', 'Tiptree', 'Maldon'],
        'Maldon':   ['Feering', 'Tiptree', 'Claston'],
        'Claston':  ['Maldon', 'Tiptree', 'Harwich']
    }
Result= ['Dunwich', 'Harwich', 'Claston', 'Maldon']

output = {x: D[x] for x in Result}

print(output)
# {'Dunwich': ['Blaxhall', 'Harwich'],
#  'Harwich': ['Dunwich', 'Blaxhall', 'Tiptree', 'Claston'],
#  'Maldon':  ['Feering', 'Tiptree', 'Claston'],
#  'Claston': ['Maldon', 'Tiptree', 'Harwich']}

CodePudding user response:

here is the solution:

D = {
    'Dunwich': ['Blaxhall', 'Harwich'],
    'Blaxhall': ['Dunwich', 'Harwich', 'Feering'],
    'Harwich': ['Dunwich', 'Blaxhall', 'Tiptree', 'Claston'],
    'Tiptree': ['Feering', 'Harwich', 'Claston', 'Maldon'],
    'Feering': ['Blaxhall', 'Tiptree', 'Maldon'],
    'Maldon': ['Feering', 'Tiptree', 'Claston'],
    'Claston': ['Maldon', 'Tiptree', 'Harwich']

}

Result = ['Dunwich', 'Harwich', 'Claston', 'Maldon']

output = dict()
for i in Result:
    output[i] = D[i]

print(output)

CodePudding user response:

There are a lot of different ways, you can use this one

D = {
            'Dunwich': ['Blaxhall', 'Harwich'],
            'Blaxhall': ['Dunwich', 'Harwich', 'Feering'],
            'Harwich': ['Dunwich', 'Blaxhall', 'Tiptree', 'Claston'],
            'Tiptree': ['Feering', 'Harwich', 'Claston', 'Maldon'],
            'Feering': ['Blaxhall', 'Tiptree', 'Maldon'],
            'Maldon': ['Feering', 'Tiptree', 'Claston'],
            'Claston': ['Maldon', 'Tiptree', 'Harwich']

        }
Result= ['Dunwich', 'Harwich', 'Claston', 'Maldon']

for k in set(D) - set(Result):
    del D[k]
    
print(D)
  • Related