Hello.
I have a (main) list of objects 'feedbacks' which have an 'order_id' and a 'message'. I have another list of simply 'order_id's whose correspondent object in the main list I would like to see removed.

For example:
main list: [object type 'feedback', object type 'feedback', object type 'feedback']
let's assume the orders id were '123', '456, '789'.

exclude_list:

Result: [object type 'feedback'] whose order_id would be '456'.

I would also like to be able to do the opposite.

Considering the same main list as before, if I had a include_list:
Result: [object type 'feedback', object type 'feedback'] whose order_ids would be '123' and '456'

I hope I have been clear enough. If I haven't just let me know and I will try to better explain myself.

I am aware that this is quite simple but I am not being able to do it successfully.

Here's the code I have for this, that isn't working:

for feedback in feedbacks:
         if feedback.order_id in orders_excluded:        # if a feedback order id is in the exclude list file
                feedbacks.remove(feedback)                  # erase it

For some unknown reason this piece of code is not removing all entries as it should.

Any help on this would be greatly appreciated.

It's best to use a set and also generator expressions like this

exclude_set = set(exclude_list)
print [f for f in main_list if not (f.order_id in exclude_set)]

Edited 7 Years Ago by Gribouillis: typo

It's best to use a set and also generator expressions like this

exclude_set = set(exclude_list)
print [f for f in main_list if not (f.order_id in exclude_set)]

Even though I have been messing around with python for some time now, I still wasn't aware of this type 'set'. This is surely a better way to do it than as I was trying. So thanks. I was already able to adapt it to the include part too.

Thanks.

You should not remove elements from a list you are looping on

list=[1,2,3,4,5,6,7,8,9]
for i in list:
    if i in [4,5,6]:
        list.remove(i)
print list

> [1, 2, 3, 5, 7, 8, 9]

When you remove an element, the followings ranks are modified (minus 1) so some elements are never used (those that takes the ranks of removed elements).
You have to loop on a copy of your list :

list=[1,2,3,4,5,6,7,8,9]
for i in list[:]: # list[:] is a copy of list...
    if i in [4,5,6]:
        list.remove(i)
print list

> [1, 2, 3, 7, 8, 9]

Edited 7 Years Ago by jice: n/a

Comments
Excellent post! Very simple and clarifying.

You should not remove elements from a list you are looping on

list=[1,2,3,4,5,6,7,8,9]
for i in list:
    if i in [4,5,6]:
        list.remove(i)
print list

> [1, 2, 3, 5, 7, 8, 9]

When you remove an element, the followings ranks are modified (minus 1) so some elements are never used (those that takes the ranks of removed elements).
You have to loop on a copy of your list :

list=[1,2,3,4,5,6,7,8,9]
for i in list[:]: # list[:] is a copy of list...
    if i in [4,5,6]:
        list.remove(i)
print list

> [1, 2, 3, 7, 8, 9]

That explains perfectly why I wasn't being able to remove the elements successfully. Thanks a lot for your post.

This question has already been answered. Start a new discussion instead.