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:
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.
CodePudding user response:
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')]