Search code examples
pythondictionaryemoji

How to delete elements with just emojis in a dictionary with Python


When I have a dict like:

dict = {'xyz': 'Sooo ein schönes Bild von dir 😍👌💋💋', 'xyz2': '✨✨✨', 'xyz3': 'toll, super schön'}

How can I use "emoji" to just find the element with emojis only (namely here the value of key xyz2) and delete it from the dict?

I use the emoji module for Python. https://pypi.org/project/emoji/


Solution

  • First consider how to detect emoji with the 'official' emoji package function demojize. It can decode emojis inside the string into tokens, so we only need to detect are there any non-token elements. Each token is a string that starts and ends with a colon, for example :smile: or :heart:. So if demojized string does not start from token(semicolon), or a token is followed by non token - that's sign of non emoji presence.

    def are_emojis(s):
        s = s.replace(":", "x")  #  colons from the original text might intefere with token markup
        s = emoji.demojize(s)
        if not s.startswith(':') or len(s) < 3:
            return False     # first symbol is not emoji
        s = s[1:-1] # drop the initial and the last colon
        s = s.replace("::", "")  # delete double colons
        return ":" not in s
    
    
    for k in list(d):
        if are_emojis(d[k]):
            del d[k]
    

    Probably, a more intuitive solution would be to use non-documented regex emoji matcher from that package, or, for simple cases even the list of emoji as below

    from emoji import UNICODE_EMOJI
    
    
    def is_emoji(s):
        return s in UNICODE_EMOJI
    
    
    for k in list(dict):
        if all(map(is_emoji, dict[k])):
            del dict[k]
    

    Regretfully, it fails in the case of multi-symbol emoji sequences, when emoji are used along with modifiers or combiner, for example skin color or zero width joiner. A workaround is to add combiners and modifier into that emoji list, but then program will also delete strings which consist only of modifiers and combiners. Better approach it to use emoji package regex.

    If you prefer build new dictionary without some elements see recipes at https://thispointer.com/python-filter-a-dictionary-by-conditions-on-keys-or-values/