How to extract all the emojis from text?

tumbleweed picture tumbleweed ยท Mar 31, 2017 ยท Viewed 41.8k times ยท Source

Consider the following list:

a_list = ['๐Ÿค” ๐Ÿ™ˆ me asรญ, bla es se ๐Ÿ˜Œ ds ๐Ÿ’•๐Ÿ‘ญ๐Ÿ‘™']

How can I extract in a new list all the emojis inside a_list?:

new_lis = ['๐Ÿค” ๐Ÿ™ˆ ๐Ÿ˜Œ ๐Ÿ’• ๐Ÿ‘ญ ๐Ÿ‘™']

I tried to use regex, but I do not have all the possible emojis encodings.

Answer

Pedro Castilho picture Pedro Castilho ยท Mar 31, 2017

You can use the emoji library. You can check if a single codepoint is an emoji codepoint by checking if it is contained in emoji.UNICODE_EMOJI.

import emoji

def extract_emojis(s):
  return ''.join(c for c in s if c in emoji.UNICODE_EMOJI)