"""
Script to print all Unicode flag emoji which are also a valid flag when reversed.

Output of this script:

๐Ÿ‡ฆ๐Ÿ‡ฌ (Antigua and Barbuda) reverses to ๐Ÿ‡ฌ๐Ÿ‡ฆ (Gabon)
๐Ÿ‡ฆ๐Ÿ‡ฑ (Albania) reverses to ๐Ÿ‡ฑ๐Ÿ‡ฆ (Lao People's Democratic Republic)
๐Ÿ‡ฆ๐Ÿ‡ฒ (Armenia) reverses to ๐Ÿ‡ฒ๐Ÿ‡ฆ (Morocco)
๐Ÿ‡ฆ๐Ÿ‡ถ (Antarctica) reverses to ๐Ÿ‡ถ๐Ÿ‡ฆ (Qatar)
๐Ÿ‡ฆ๐Ÿ‡ธ (American Samoa) reverses to ๐Ÿ‡ธ๐Ÿ‡ฆ (Saudi Arabia)
๐Ÿ‡ฆ๐Ÿ‡บ (Australia) reverses to ๐Ÿ‡บ๐Ÿ‡ฆ (Ukraine)
๐Ÿ‡ฆ๐Ÿ‡ฟ (Azerbaijan) reverses to ๐Ÿ‡ฟ๐Ÿ‡ฆ (South Africa)
๐Ÿ‡ง๐Ÿ‡ง (Barbados) reverses to ๐Ÿ‡ง๐Ÿ‡ง (Barbados)
๐Ÿ‡ง๐Ÿ‡ฌ (Bulgaria) reverses to ๐Ÿ‡ฌ๐Ÿ‡ง (United Kingdom)
๐Ÿ‡ง๐Ÿ‡ฑ (Saint Barthรฉlemy) reverses to ๐Ÿ‡ฑ๐Ÿ‡ง (Lebanon)
๐Ÿ‡ง๐Ÿ‡ธ (Bahamas) reverses to ๐Ÿ‡ธ๐Ÿ‡ง (Solomon Islands)
๐Ÿ‡จ๐Ÿ‡จ (Cocos (Keeling) Islands) reverses to ๐Ÿ‡จ๐Ÿ‡จ (Cocos (Keeling) Islands)
๐Ÿ‡จ๐Ÿ‡ฑ (Chile) reverses to ๐Ÿ‡ฑ๐Ÿ‡จ (Saint Lucia)
๐Ÿ‡จ๐Ÿ‡ฒ (Cameroon) reverses to ๐Ÿ‡ฒ๐Ÿ‡จ (Monaco)
๐Ÿ‡จ๐Ÿ‡ณ (China) reverses to ๐Ÿ‡ณ๐Ÿ‡จ (New Caledonia)
๐Ÿ‡จ๐Ÿ‡ป (Cape Verde) reverses to ๐Ÿ‡ป๐Ÿ‡จ (Saint Vincent and the Grenadines)
๐Ÿ‡ฉ๐Ÿ‡ฒ (Dominica) reverses to ๐Ÿ‡ฒ๐Ÿ‡ฉ (Moldova, Republic of)
๐Ÿ‡ช๐Ÿ‡ช (Estonia) reverses to ๐Ÿ‡ช๐Ÿ‡ช (Estonia)
๐Ÿ‡ช๐Ÿ‡ฌ (Egypt) reverses to ๐Ÿ‡ฌ๐Ÿ‡ช (Georgia)
๐Ÿ‡ช๐Ÿ‡ท (Eritrea) reverses to ๐Ÿ‡ท๐Ÿ‡ช (Rรฉunion)
๐Ÿ‡ช๐Ÿ‡ธ (Spain) reverses to ๐Ÿ‡ธ๐Ÿ‡ช (Sweden)
๐Ÿ‡ซ๐Ÿ‡ฒ (Micronesia, Federated States of) reverses to ๐Ÿ‡ฒ๐Ÿ‡ซ (Saint Martin (French part))
๐Ÿ‡ฌ๐Ÿ‡ฆ (Gabon) reverses to ๐Ÿ‡ฆ๐Ÿ‡ฌ (Antigua and Barbuda)
๐Ÿ‡ฌ๐Ÿ‡ง (United Kingdom) reverses to ๐Ÿ‡ง๐Ÿ‡ฌ (Bulgaria)
๐Ÿ‡ฌ๐Ÿ‡ช (Georgia) reverses to ๐Ÿ‡ช๐Ÿ‡ฌ (Egypt)
๐Ÿ‡ฌ๐Ÿ‡ฌ (Guernsey) reverses to ๐Ÿ‡ฌ๐Ÿ‡ฌ (Guernsey)
๐Ÿ‡ฌ๐Ÿ‡ฒ (Gambia) reverses to ๐Ÿ‡ฒ๐Ÿ‡ฌ (Madagascar)
๐Ÿ‡ฌ๐Ÿ‡ณ (Guinea) reverses to ๐Ÿ‡ณ๐Ÿ‡ฌ (Nigeria)
๐Ÿ‡ฌ๐Ÿ‡ต (Guadeloupe) reverses to ๐Ÿ‡ต๐Ÿ‡ฌ (Papua New Guinea)
๐Ÿ‡ฌ๐Ÿ‡ธ (South Georgia and the South Sandwich Islands) reverses to ๐Ÿ‡ธ๐Ÿ‡ฌ (Singapore)
๐Ÿ‡ฌ๐Ÿ‡น (Guatemala) reverses to ๐Ÿ‡น๐Ÿ‡ฌ (Togo)
๐Ÿ‡ฌ๐Ÿ‡บ (Guam) reverses to ๐Ÿ‡บ๐Ÿ‡ฌ (Uganda)
๐Ÿ‡ญ๐Ÿ‡ฐ (Hong Kong) reverses to ๐Ÿ‡ฐ๐Ÿ‡ญ (Cambodia)
๐Ÿ‡ญ๐Ÿ‡ฒ (Heard Island and McDonald Islands) reverses to ๐Ÿ‡ฒ๐Ÿ‡ญ (Marshall Islands)
๐Ÿ‡ญ๐Ÿ‡น (Haiti) reverses to ๐Ÿ‡น๐Ÿ‡ญ (Thailand)
๐Ÿ‡ฎ๐Ÿ‡ฑ (Israel) reverses to ๐Ÿ‡ฑ๐Ÿ‡ฎ (Liechtenstein)
๐Ÿ‡ฎ๐Ÿ‡ณ (India) reverses to ๐Ÿ‡ณ๐Ÿ‡ฎ (Nicaragua)
๐Ÿ‡ฎ๐Ÿ‡ธ (Iceland) reverses to ๐Ÿ‡ธ๐Ÿ‡ฎ (Slovenia)
๐Ÿ‡ฐ๐Ÿ‡ญ (Cambodia) reverses to ๐Ÿ‡ญ๐Ÿ‡ฐ (Hong Kong)
๐Ÿ‡ฐ๐Ÿ‡ฒ (Comoros) reverses to ๐Ÿ‡ฒ๐Ÿ‡ฐ (Macedonia, the Former Yugoslav Republic of)
๐Ÿ‡ฐ๐Ÿ‡ต (Korea, Democratic People's Republic of) reverses to ๐Ÿ‡ต๐Ÿ‡ฐ (Pakistan)
๐Ÿ‡ฑ๐Ÿ‡ฆ (Lao People's Democratic Republic) reverses to ๐Ÿ‡ฆ๐Ÿ‡ฑ (Albania)
๐Ÿ‡ฑ๐Ÿ‡ง (Lebanon) reverses to ๐Ÿ‡ง๐Ÿ‡ฑ (Saint Barthรฉlemy)
๐Ÿ‡ฑ๐Ÿ‡จ (Saint Lucia) reverses to ๐Ÿ‡จ๐Ÿ‡ฑ (Chile)
๐Ÿ‡ฑ๐Ÿ‡ฎ (Liechtenstein) reverses to ๐Ÿ‡ฎ๐Ÿ‡ฑ (Israel)
๐Ÿ‡ฑ๐Ÿ‡ธ (Lesotho) reverses to ๐Ÿ‡ธ๐Ÿ‡ฑ (Sierra Leone)
๐Ÿ‡ฑ๐Ÿ‡น (Lithuania) reverses to ๐Ÿ‡น๐Ÿ‡ฑ (Timor-Leste)
๐Ÿ‡ฒ๐Ÿ‡ฆ (Morocco) reverses to ๐Ÿ‡ฆ๐Ÿ‡ฒ (Armenia)
๐Ÿ‡ฒ๐Ÿ‡จ (Monaco) reverses to ๐Ÿ‡จ๐Ÿ‡ฒ (Cameroon)
๐Ÿ‡ฒ๐Ÿ‡ฉ (Moldova, Republic of) reverses to ๐Ÿ‡ฉ๐Ÿ‡ฒ (Dominica)
๐Ÿ‡ฒ๐Ÿ‡ซ (Saint Martin (French part)) reverses to ๐Ÿ‡ซ๐Ÿ‡ฒ (Micronesia, Federated States of)
๐Ÿ‡ฒ๐Ÿ‡ฌ (Madagascar) reverses to ๐Ÿ‡ฌ๐Ÿ‡ฒ (Gambia)
๐Ÿ‡ฒ๐Ÿ‡ญ (Marshall Islands) reverses to ๐Ÿ‡ญ๐Ÿ‡ฒ (Heard Island and McDonald Islands)
๐Ÿ‡ฒ๐Ÿ‡ฐ (Macedonia, the Former Yugoslav Republic of) reverses to ๐Ÿ‡ฐ๐Ÿ‡ฒ (Comoros)
๐Ÿ‡ฒ๐Ÿ‡ฒ (Myanmar) reverses to ๐Ÿ‡ฒ๐Ÿ‡ฒ (Myanmar)
๐Ÿ‡ฒ๐Ÿ‡ด (Macao) reverses to ๐Ÿ‡ด๐Ÿ‡ฒ (Oman)
๐Ÿ‡ฒ๐Ÿ‡ต (Northern Mariana Islands) reverses to ๐Ÿ‡ต๐Ÿ‡ฒ (Saint Pierre and Miquelon)
๐Ÿ‡ฒ๐Ÿ‡ธ (Montserrat) reverses to ๐Ÿ‡ธ๐Ÿ‡ฒ (San Marino)
๐Ÿ‡ฒ๐Ÿ‡น (Malta) reverses to ๐Ÿ‡น๐Ÿ‡ฒ (Turkmenistan)
๐Ÿ‡ฒ๐Ÿ‡บ (Mauritius) reverses to ๐Ÿ‡บ๐Ÿ‡ฒ (United States Minor Outlying Islands)
๐Ÿ‡ฒ๐Ÿ‡ฟ (Mozambique) reverses to ๐Ÿ‡ฟ๐Ÿ‡ฒ (Zambia)
๐Ÿ‡ณ๐Ÿ‡จ (New Caledonia) reverses to ๐Ÿ‡จ๐Ÿ‡ณ (China)
๐Ÿ‡ณ๐Ÿ‡ฌ (Nigeria) reverses to ๐Ÿ‡ฌ๐Ÿ‡ณ (Guinea)
๐Ÿ‡ณ๐Ÿ‡ฎ (Nicaragua) reverses to ๐Ÿ‡ฎ๐Ÿ‡ณ (India)
๐Ÿ‡ณ๐Ÿ‡ต (Nepal) reverses to ๐Ÿ‡ต๐Ÿ‡ณ (Pitcairn)
๐Ÿ‡ด๐Ÿ‡ฒ (Oman) reverses to ๐Ÿ‡ฒ๐Ÿ‡ด (Macao)
๐Ÿ‡ต๐Ÿ‡ฌ (Papua New Guinea) reverses to ๐Ÿ‡ฌ๐Ÿ‡ต Guadeloupe)
๐Ÿ‡ต๐Ÿ‡ฐ (Pakistan) reverses to ๐Ÿ‡ฐ๐Ÿ‡ต (Korea, Democratic People's Republic of)
๐Ÿ‡ต๐Ÿ‡ฒ (Saint Pierre and Miquelon) reverses to ๐Ÿ‡ฒ๐Ÿ‡ต (Northern Mariana Islands)
๐Ÿ‡ต๐Ÿ‡ณ (Pitcairn) reverses to ๐Ÿ‡ณ๐Ÿ‡ต (Nepal)
๐Ÿ‡ถ๐Ÿ‡ฆ (Qatar) reverses to ๐Ÿ‡ฆ๐Ÿ‡ถ (Antarctica)
๐Ÿ‡ท๐Ÿ‡ช (Rรฉunion) reverses to ๐Ÿ‡ช๐Ÿ‡ท (Eritrea)
๐Ÿ‡ท๐Ÿ‡ธ (Serbia) reverses to ๐Ÿ‡ธ๐Ÿ‡ท (Suriname)
๐Ÿ‡ธ๐Ÿ‡ฆ (Saudi Arabia) reverses to ๐Ÿ‡ฆ๐Ÿ‡ธ (American Samoa)
๐Ÿ‡ธ๐Ÿ‡ง (Solomon Islands) reverses to ๐Ÿ‡ง๐Ÿ‡ธ (Bahamas)
๐Ÿ‡ธ๐Ÿ‡ช (Sweden) reverses to ๐Ÿ‡ช๐Ÿ‡ธ (Spain)
๐Ÿ‡ธ๐Ÿ‡ฌ (Singapore) reverses to ๐Ÿ‡ฌ๐Ÿ‡ธ (South Georgia and the South Sandwich Islands)
๐Ÿ‡ธ๐Ÿ‡ฎ (Slovenia) reverses to ๐Ÿ‡ฎ๐Ÿ‡ธ (Iceland)
๐Ÿ‡ธ๐Ÿ‡ฑ (Sierra Leone) reverses to ๐Ÿ‡ฑ๐Ÿ‡ธ (Lesotho)
๐Ÿ‡ธ๐Ÿ‡ฒ (San Marino) reverses to ๐Ÿ‡ฒ๐Ÿ‡ธ (Montserrat)
๐Ÿ‡ธ๐Ÿ‡ท (Suriname) reverses to ๐Ÿ‡ท๐Ÿ‡ธ (Serbia)
๐Ÿ‡ธ๐Ÿ‡ธ (South Sudan) reverses to ๐Ÿ‡ธ๐Ÿ‡ธ (South Sudan)
๐Ÿ‡น๐Ÿ‡ฌ (Togo) reverses to ๐Ÿ‡ฌ๐Ÿ‡น (Guatemala)
๐Ÿ‡น๐Ÿ‡ญ (Thailand) reverses to ๐Ÿ‡ญ๐Ÿ‡น (Haiti)
๐Ÿ‡น๐Ÿ‡ฑ (Timor-Leste) reverses to ๐Ÿ‡ฑ๐Ÿ‡น (Lithuania)
๐Ÿ‡น๐Ÿ‡ฒ (Turkmenistan) reverses to ๐Ÿ‡ฒ๐Ÿ‡น (Malta)
๐Ÿ‡น๐Ÿ‡น (Trinidad and Tobago) reverses to ๐Ÿ‡น๐Ÿ‡น (Trinidad and Tobago)
๐Ÿ‡บ๐Ÿ‡ฆ (Ukraine) reverses to ๐Ÿ‡ฆ๐Ÿ‡บ (Australia)
๐Ÿ‡บ๐Ÿ‡ฌ (Uganda) reverses to ๐Ÿ‡ฌ๐Ÿ‡บ (Guam)
๐Ÿ‡บ๐Ÿ‡ฒ (United States Minor Outlying Islands) reverses to ๐Ÿ‡ฒ๐Ÿ‡บ (Mauritius)
๐Ÿ‡ป๐Ÿ‡จ (Saint Vincent and the Grenadines) reverses to ๐Ÿ‡จ๐Ÿ‡ป (Cape Verde)
๐Ÿ‡ฟ๐Ÿ‡ฆ (South Africa) reverses to ๐Ÿ‡ฆ๐Ÿ‡ฟ (Azerbaijan)
๐Ÿ‡ฟ๐Ÿ‡ฒ (Zambia) reverses to ๐Ÿ‡ฒ๐Ÿ‡ฟ (Mozambique)
"""
import json
from urllib.request import urlopen
import unicodedata


# JSON list of ISO 3166-1-alpha-2 data
country_data_url = "https://www.datahub.io/core/country-list/r/data.json"
country_data = json.loads(urlopen(country_data_url).read().decode())


# Map country codes to country names
countries = {country["Code"]: country["Name"] for country in country_data}


# Find all country codes that are valid when reversed
reversible_codes = sorted(code for code in countries if code[::-1] in countries)


def flag_for(code):
    """Return unicode flag emoji given a 2-digit country code."""
    return "".join(
        unicodedata.lookup(f"REGIONAL INDICATOR SYMBOL LETTER {char}") for char in code
    )


# Print each flag and country name and its valid reverse flag and country name
# Note: some flags are their own reverse (due to palindrome codes like TT)
# Note: each countries is printed twice (once as forward, once as reverse)
for code in reversible_codes:
    print(
        f"{flag_for(code)} ({countries[code]})",
        "reverses to",
        f"{flag_for(code[::-1])} ({countries[code[::-1]]})",
    )

40 views

Share

pym.dev/p/2w4by/

Need to share some Python code?

Create a new snippet