How to combine for loop and if-statement with two values

Viewed 53

I need to run compare between two functions and if the functions output not equal, statement will trigger function with specific job number. If equal will pass. I need to run this if-statement for 8 different repos with 8 different ids. Code example:

#first_app_container(7)                                                                                                                                                       
if ecr_image_ver('first_app_container') != server_image_ver('first_app_container'):                                                                             
    activate_image_download(7)                                                                                                                                                 
else:                                                                                                                                                                               
    pass  

#second_app_container(14)                                                                                                                                                       
if ecr_image_ver('second_app_container') != server_image_ver('second_app_container'):                                                                             
    activate_image_download(14)                                                                                                                                                 
else:                                                                                                                                                                               
    pass

#third_app_container(3)                                                                                                                                                       
if ecr_image_ver('third_app_container') != server_image_ver('third_app_container'):                                                                             
    activate_image_download(3)                                                                                                                                                 
else:                                                                                                                                                                               
    pass                                                                                                                                                                          
                                                                                                                                 

I'm pretty new on Python and wonder how can i make it with if-statement and for/while loop?

Thank you.

2 Answers

You can store the job numbers in a dictionary, and then loop through them like this:

job_numbers = {'first_app_container': 7,
               'second_app_container': 14,
               'third_app_container': 3}

for name, job_number in job_numbers.items():
    if ecr_image_ver(name) != server_image_ver(name):
        activate_image_download(job_number)

The best way to store the data will depend on your project, but generally using an array of dictionaries will scale as you need more key/value pairs. If you are already familiar with other languages, you can learn the syntax for common data structures using https://learnxinyminutes.com/docs/python/

For example you could do something like this. Which is very similar to @jprebys answer. The benefit is you can add more data per job.

jobs = [
    {
        "name": "first_app_container",
        "extra": "first_extra",
        "id": 7,
    },
    {
        "name": "second_app_container",
        "extra": "second_extra",
        "id": 14,
    },
    {
        "name": "third_app_container",
        "extra": "third_extra",
        "id": 3,
    }
]

for job in jobs:
    if ecr_image_ver(job["name"]) != server_image_ver(job["name"]):
        activate_image_download(job["id"])
Related