Step 3 - List Comprehensions

So far our examples have provided a list of items to iterate over. What if we want to create a new list out of the data provided. For example, maybe we want to generate a list of just the text we want to translate.

You could do this using a built-in python method .append() and a for loop.

  • Add this function to your code in
# Create a list of the input text
def new_input_text_list():
    input_text = open_input()
    new_list = []
    for item in input_text:
        text = item['Text']

Remember to call the function from your main() otherwise it won’t run.

def main():
  • To run the program, enter the following command in the terminal:

    python --file translate_input.json

This should return the following:

['What is cloud computing?', 'Cloud computing is the on-demand delivery of IT resources over the Internet with pay-as-you-go pricing.', 'Instead of buying, owning, and maintaining physical data centers and servers, you can access technology services, such as computing power, storage, and databases, on an as-needed basis from a cloud provider like Amazon Web Services (AWS)', 'Who is using cloud computing?', 'Organizations of every type, size, and industry are using the cloud for a wide variety of use cases, such as data backup, disaster recovery, email, virtual desktops, software development and testing, big data analytics, and customer-facing web applications.', 'For example, healthcare companies are using the cloud to develop more personalized treatments for patients. Financial services companies are using the cloud to power real-time fraud detection and prevention.', 'And video game makers are using the cloud to deliver online games to millions of players around the world.']
  • You can see that this is a list as it is wrapped in []

List Comprehensions

Using a for loop to generate new lists using .append() is a perfectly valid way of generating new lists. However, we can use a list comprehension to reduce this to a single line. It combines the for loop and the creation of the list into a single line.

  1. In add the following below the new_input_text_list() function:
def new_list_comprehension():
    input_text = open_input()
    list_comprehension = [item['Text'] for item in input_text]
  1. Add the function to main()
def main():
  • To run the program, enter the following command in the terminal:

    python --file translate_input.json

You will see that it performs the same function as the for loop but on a single line. Although these look complex to start with you can break down the structure to make it easier to remember.

  1. The list comprehension is assigned to a variable called list_comprehension.

  2. Because it is creating a list it is wrapped in [], this replaces the line new_list = [] which was used to create an empty list.

  3. The part after the first part defines what specific value we want to include in the list item['Text'] which replaces text = item['Text'] which removes assigning it to a variable.

  4. The second part of the expression is the for loop for item in input_text which is identical to the original for loop for item in input_text:

You will see list comprehensions used in other code, so if you don’t decide to use them, you will at least be able to determine what they are doing.