There is a list of all_users, in this list there are dictionaries - person, a person has a parameter called user_id. I don't want to add dictionaries with the same user_id to the list.
all_users = [] while True: for event in longpoll.listen(): if event.type == VkEventType.MESSAGE_NEW and not (event.from_me): person = dict.fromkeys(['user_id']) person['user_id'] = event.user_id all_users.append(person) for x in all_users: print(x) This code assigns the id of the person who wrote the message to the person as the value for the key user_id, later adding the person to the list all_users
for it in all_users: if it['user_id'] != person['user_id']: all_users.append(person) print("Добавил!") else: print("Этот пользователь уже добавлен") Having added this code before I displayed all_users on the screen, I thought that the person with the same user_id would no longer be added to the list, but this code was not executed at all + the entire list was not displayed on the screen.
I can not understand what the problem is. Tell me, please, is it correct if I check for a user_id match, if it is correct, then why does the code stop running? and if wrong, how can the implementation be done differently?
all_users_ids = {user['user_id'] for user in all_users} if user_id not in all_users_ids: person = {'user_id': user_id, 'link': link} all_users.append(person) print("Добавил") for x in all_users: print(x)