Home > Back-end >  remove items from the dictionary having a common element in list
remove items from the dictionary having a common element in list

Time:07-27

I have one dictionary and one list values I want to remove the values from the dictionary if the element is existed in list data , I worked with some methods but some values are not removing, not getting where i did mistake

extra_list = ['387', '404', '637', '401', '405', '579', '408', '739', '736', '447', '403', '402', '581']
main_data = [{'stock id': 447, 'name': 'Inners(Essa fai', 'values': 4},
             {'stock id': 757, 'name': 'Designery(Blous', 'values': 2},
             {'stock id': 652, 'name': 'Saree krishna(P', 'values': 1},
             {'stock id': 408, 'name': 'Designery(Blous', 'values': 1},
             {'stock id': 684, 'name': 'Saree(Ikkat pri', 'values': 1}]


def listRemover(main_list, remove_list):
    for j in main_list:
        if str(j['stock id']) in remove_list:
            main_list.remove(j)
    return main_list


data = listRemover(main_data, extra_list)

I want remove the dictionary value if the stock id existed in extra list

CodePudding user response:

I would create a new list in memory of what you want to keep. As stated in the comments it's a bad idea to modiy a list your looping over

def listRemover(main_list, remove_list):
    new_list = []
    for j in main_list:
        if not (str(j['stock id']) in remove_list):
            new_list.append(j)
    return new_list


data = listRemover(main_data, extra_list)

Output

[{'stock id': 757, 'name': 'Designery(Blous', 'values': 2}, {'stock id': 652, 'name': 'Saree krishna(P', 'values': 1}, {'stock id': 684, 'name': 'Saree(Ikkat pri', 'values': 1}]

CodePudding user response:

Rather than modifying the input list, create a new list as follows:

extra_list = ['387', '404', '637', '401', '405', '579', '408', '739', '736', '447', '403', '402', '581']
main_data = [{'stock id': 447, 'name': 'Inners(Essa fai', 'values': 4},
             {'stock id': 757, 'name': 'Designery(Blous', 'values': 2},
             {'stock id': 652, 'name': 'Saree krishna(P', 'values': 1},
             {'stock id': 408, 'name': 'Designery(Blous', 'values': 1},
             {'stock id': 684, 'name': 'Saree(Ikkat pri', 'values': 1}]


def listRemover(main_list, remove_list):
    s = set(map(int, remove_list))
    return [j for j in main_list if j.get('stock id') not in s]

print(listRemover(main_data, extra_list))

Output:

[{'stock id': 757, 'name': 'Designery(Blous', 'values': 2}, {'stock id': 652, 'name': 'Saree krishna(P', 'values': 1}, {'stock id': 684, 'name': 'Saree(Ikkat pri', 'values': 1}]

CodePudding user response:

I ran your code and printed data, which gave me:

[{'stock id': 757, 'name': 'Designery(Blous', 'values': 2}, {'stock id': 652, 'name': 'Saree krishna(P', 'values': 1}, {'stock id': 684, 'name': 'Saree(Ikkat pri', 'values': 1}]

so I guess it worked.

However, removing objects from a list you're iterating over is usually not the best idea. You could do

for j in copy.deepcopy(main_list):

instead of:

for j in main_list:

just to be safe.

  • Related