I have a form that expects a unique 5-character ID. I have a function that generates this unique ID. Before entering it in the form, I want to compare it with an existing list of IDs. If the generated ID doesn't exist in the list, pass it in a variable; if it does exist and it isn't unique, generate another ID. What's the best way to go about this?

```py
def generate_id():
    random_id= ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(5))
    return random_id

existing_ids = ['AAAAA', 'BBBBB', 'CCCCC']
for id in existing_ids:
    if not generate_id() == id:
        unique_id = generate_id()

```