0

I'm creating a dynamic array of UUIDs, and I have another list of existing UUIDs, I want to delete items from the existing list that aren't in the new dynamic list. I'm trying to do this like this

# Remove components that aren't being updated
new_component_id_for_existing_sections = []
for component in new_components:
    if component.get('section_holder_id'):
        new_component_id_for_existing_sections.append(component.get('component_id'))

    print('New component IDs')
    for com in new_component_id_for_existing_sections:
        print(com)

    print('Checking existing components')
    for existing_component in self.get_object().components.all():
        print(existing_component.component.id)
        print(existing_component.component.id not in new_component_id_for_existing_sections)

So in here I create the array new_component_id_for_existing_sections which in my example has two IDs in, and self.get_object().components.all() has 3 ids in. But the output for this gives me.

New component IDs
acae9374-d32d-4752-ba5a-9437a54dbbe7
a2a9d893-86ba-4d1d-938e-f638e7b2a4b2

Checking existing components
f3cb3cc6-4d66-4df5-8232-2c1f858c8632 <----Not in the array but returns that it is
True 

acae9374-d32d-4752-ba5a-9437a54dbbe7
True

a2a9d893-86ba-4d1d-938e-f638e7b2a4b2
True

The first item is saying it's in the array, but it isn't and I can't figure out why

7
  • 1
    Why not use set and take difference? Commented Aug 8, 2020 at 14:35
  • I'm quite a novice when it comes to Python, do you mean create two sets and compare them? Commented Aug 8, 2020 at 14:38
  • 2
    That says that none of those are in new_component_id_for_existing_sections. From the looks of it, the first one should print True, and the other two False – not the other way around. Commented Aug 8, 2020 at 14:39
  • Ah yeah, either way though something strange is happening and I can't figure out what's up with it. Does python do anything special when comparing strings? Commented Aug 8, 2020 at 14:43
  • 1
    Oh @DanielSims I got it! I am sorry it was my confusion. Commented Aug 8, 2020 at 15:38

1 Answer 1

1

Turns out this was a type issue, existing_component.component.id is a UUID, and the array items are strings, and it didn't like comparing UUID -> Strings.

Adding this solved the issue existing_component.component.id.__str__()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.