def union_favourites_and_cards(favourites, cards):
    """
        Function to unite user's favourites cards with a queryset of cards
    """
    # Using sets due to the fact that time complexity is O(1)
    # to check that an element is in the set
    favourites_set = set()
    resulting_cards = cards.all()

    for favour in favourites:
        favourites_set.add(favour.card.id)

    for card in resulting_cards:
        # Add new field to the card with the flag
        # to indicate is a card user's favourite or not
        if card.id in favourites_set:
            card.favourite = True
        else:
            card.favourite = False

    return resulting_cards