11

Is it possible to get the list of all ISO639-1 language codes from pycountry 1.15? For example, ['en','it','el','fr',...]? If yes then how?

The following doesn't work I am afraid:

import pycountry
pycountry.languages
user706838
  • 5,132
  • 14
  • 54
  • 78
  • Do you want ISO 3166 country codes or ISO 639 language codes? As far as I know there is no such thing as a "ISO 3166-2 language code." – Robᵩ Sep 25 '15 at 01:48

2 Answers2

13

This will give you a list of two-digit ISO3166-1 country codes:

countries = [country.alpha2 for country in pycountry.countries]
#countries = [country.alpha_2 for country in pycountry.countries]  # for python3 

This will give you a list of two-digit ISO639-1 language codes:

langs = [lang.iso639_1_code
         for lang in pycountry.languages
         if hasattr(lang, 'iso639_1_code')]

This will give you a list of all of the language codes:

langs = [getattr(lang, 'iso639_1_code', None) or 
         getattr(lang, 'iso639_2T_code', None) or 
         getattr(lang, 'iso639_3_code') 
         for lang in pycountry.languages]
AnnieFromTaiwan
  • 3,845
  • 3
  • 22
  • 38
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Nice. `langs = [(lang.iso639_1_code, lang.name) for...` could be used to generate data for a language selection widget. If it'd only run on app startup, than there's no need for hardcoding a list of languages or keeping a separate database table. – fmalina Feb 24 '16 at 13:58
  • 6
    It's *alpha_2*, at least on Python 3. – Georg Pfolz Sep 09 '20 at 09:32
1

For Python3 its:

countries = [country.alpha_2 for country in pycountry.countries]
Ashwin Das
  • 11
  • 3
  • 2
    This doesn't contain every language code, seems to omit some that are not linked to specific countries. Example: wikipedia states that 'AA' is a valid language code for the Afro-Asiatic language family, but this won't be a result here. – robert Sep 11 '20 at 02:32