Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to filter out nested elements from a nested dictionary

I have the following list of dictionaries:

messages_all = [{'type': 'message',
      'subtype': 'bot_message',
      'text': "This content can't be displayed.",
      'ts': '1573358255.000100',
      'username': 'Userform',
      'icons': {'image_30': 'www.example.com'},
      'bot_id': 'JOD4K22SJW',
      'blocks': [{'type': 'section',
        'block_id': 'yCKUB',
        'text': {'type': 'mrkdwn',
         'text': 'Your *survey* has a new response.',
         'verbatim': False}},
       {'type': 'section',
        'block_id': '37Mt4',
        'text': {'type': 'mrkdwn',
         'text': '*Thanks for your response. Where did you first hear about us?*\nFriend',
         'verbatim': False}},
       {'type': 'section',
        'block_id': 'hqps2',
        'text': {'type': 'mrkdwn',
         'text': '*How would you rate your experience?*\n9',
         'verbatim': False}},
       {'type': 'section',
        'block_id': 'rvi',
        'text': {'type': 'mrkdwn', 'text': '*city*\nNew York', 'verbatim': False}},
       {'type': 'section',
        'block_id': 'q=L+',
        'text': {'type': 'mrkdwn',
         'text': '*order_id*\n123456',
         'verbatim': False}},
       {'type': 'section',
        'block_id': 'iI6v',
        'text': {'type': 'mrkdwn',
         'text': '*user_id*\n987654',
         'verbatim': False}},
       {'type': 'section',
        'block_id': 'qfaZM',
        'text': {'type': 'mrkdwn',
         'text': '*user_name*\nJohn Smith',
         'verbatim': False}},
       {'type': 'section',
        'block_id': 'MkFq',
        'text': {'type': 'mrkdwn',
         'text': '*order_id*\n12345',
         'verbatim': False}},
       {'type': 'section',
        'block_id': '+zD',
        'text': {'type': 'mrkdwn', 'text': '*rating*\n9', 'verbatim': False}},
       {'type': 'section',
        'block_id': 'glVN',
        'text': {'type': 'mrkdwn',
         'text': '*office*\nSouth',
         'verbatim': False}},
       {'type': 'section',
        'block_id': 'Ox5',
        'text': {'type': 'mrkdwn',
         'text': '*date*\nJuly 1, 2020',
         'verbatim': False}},
       {'type': 'section',
        'block_id': 'pOZ',
        'text': {'type': 'mrkdwn',
         'text': '*ord_time*\n5:36PM',
         'verbatim': False}},
       {'type': 'section',
        'block_id': 'e1hbI',
        'text': {'type': 'mrkdwn',
         'text': '*sales*\nJ73',
         'verbatim': False}},
       {'type': 'actions',
        'block_id': '1Ug0A',
        'elements': [{'type': 'button',
          'action_id': '=iuO',
          'text': {'type': 'plain_text', 'text': 'View typeform', 'emoji': True},
          'url': 'www.example.com'}]},
       {'type': 'section',
        'block_id': 'Echk',
        'text': {'type': 'mrkdwn', 'text': ' ', 'verbatim': False}}]},
     {'client_msg_id': '123456jk-a19c-97fe-35c9-3c9f643cae19',
      'type': 'message',
      'text': '<@ABC973RJD>',
      'user': 'UM1927AJG',
      'ts': '1573323860.000300',
      'team': 'B09AJR39A',
      'reactions': [{'name': '+1', 'users': ['UM1927AJG'], 'count': 1}]}]

I’d like to be able to search through this list and filter out of the following dictionary:

 {'client_msg_id': '123456jk-a19c-97fe-35c9-3c9f643cae19',
  'type': 'message',
  'text': '<@ABC973RJD>',
  'user': 'UM1927AJG',
  'ts': '1573323860.000300',
  'team': 'B09AJR39A',
  'reactions': [{'name': '+1', 'users': ['UM1927AJG'], 'count': 1}]}

To do so, I have tried the following list comprehension:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

filtered = [elem for elem in messages_all if not (elem.get('type') == 'message' and elem.get('subtype') == 'channel_join') 
                     and not (elem.get('type') == 'message' and elem.get('subtype') == 'channel_leave')
                     and not (elem.get('type') == 'message' and elem.get('reply_users_count') == 2)
                     and not (elem.get('type') == 'message' and elem.get('reactions') is not None) ]

Unfortunately, the and not (elem.get('type') == 'message' and elem.get('reactions') is not None is not dropping the dictionary above.

Would someone kindly help me with the syntax?

Thank you.

>Solution :

Since the last dictionary is the only one that has the "client_msg_id" key, you could use:

>>> [m for m in messages_all if not m.get('client_msg_id')]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading