There are such dictionaries obtained by the cycle:

{'first_name': 'Иван', 'last_name': 'Иванов', 'online': 0} {'first_name': 'Иван', 'last_name': 'Иванов', 'online': 1} {'first_name': 'Иван', 'last_name': 'Иванов', 'online': 0} {'first_name': 'Иван', 'last_name': 'Иванов', 'online': 1} 

You need to get dictionaries that have 'online' with a value of 1. At the same time, so that whole dictionaries are returned, i.e. with first_name and last_name, and not just 'online': 1. I tried to do this:

  for search in dict: if 'online' == 1 in search: print(search) 

But the result did not give any.

  • one
    There are such dictionaries where? How and in which variables did you get them? - andreymal
  • @andreymal via vk api: dict = vk_session.method ('users.get', {'user_ids': user_ids), 'fields': 'online'}) - ANDROSHA
  • Show us print (dict) - andreymal
  • @andreymal print (dict) will display what I wrote in the question - ANDROSHA
  • one
    He generally can not deduce what you wrote in the question. You wrote four separate dictionaries, and dict is strictly one object - andreymal

1 answer 1

 items = [ {'first_name': 'Иван', 'last_name': 'Иванов', 'online': 0}, {'first_name': 'Иван', 'last_name': 'Иванов', 'online': 1}, {'first_name': 'Иван', 'last_name': 'Иванов', 'online': 0}, {'first_name': 'Иван', 'last_name': 'Иванов', 'online': 1}, ] # Фильтр new_items = [data for data in items if data['online']] for data in new_items: print(data) # Или без создания нового списка: for data in items: if data['online']: print(data) 

Console:

 {'first_name': 'Иван', 'last_name': 'Иванов', 'online': 1} {'first_name': 'Иван', 'last_name': 'Иванов', 'online': 1}