Step 2 - Checking Values in a List

Amazon Translate will only support a defined list of languages. The list of language codes that Amazon Translate will accept are provided in the Developer Guide. We can use an if statement to perform some input validation to check that the language code is in the list.

If a wrong value is provided, Amazon Translate will raise an error. We will deal with handling errors in a later chapter.

  • Create a new file called and add the following code:
import json

# A defined list of languages supported by Amazon Translate
languages = ["af","sq","am","ar","az","bn","bs","bg","zh","zh-TW","hr","cs","da","fa-AF","nl","en","et","fi","fr","fr-CA","ka","de","el","ha","he","hi","hu","id","it","ja","ko","lv","ms","no","fa","ps","pl","pt","ro","ru","sr","sk","sl","so","es","sw","sv","tl","ta","th","tr","uk","ur","vi"]

# This uses a json string as an input
json_string = """
        "Text":"I am learning to code in AWS",

json_input = json.loads(json_string)

# Extracts the SourceLanguageCode and TargetLanguageCode from the JSON
SourceLanguageCode = json_input['Input'][0]['SourceLanguageCode']
TargetLanguageCode = json_input['Input'][0]['TargetLanguageCode']

# Uses an if-else statement to check that the SourceLanguageCode is in the languages list.
if SourceLanguageCode in languages:
    print("The SourceLanguageCode is valid - proceeding")
    print("The SourceLanguageCode is not valid - stopping")
  • To run the program, enter the following command in the terminal:


In this example, we used the if statement combined with in to check if the language code for the source language is valid.

In the next step, we will look at how we can use the and statement to check both the source and target language codes. In addition we introduce an else-if statement which is abbreviated to elif