pythonlistloopsparameterized

How to use a value from one list in another list?


How would I iterate through the values in one list and use them in another? Say I have a list called fruit and another list called jobs

fruit = ['apple','pear','banana']
jobs =['clean the fruit', 'cut the fruit', 'eat the fruit']

and I want where it says fruit to be the fruits from the list fruit. So it would return 'clean the apple', 'clean the pear', 'clean the banana'


Solution

  • Use:

    fruits = ['apple','pear','banana']
    jobs =['clean the fruit', 'cut the fruit', 'eat the fruit']
    
    for job in jobs:
        for fruit in fruits:
            print(job.replace("fruit", fruit))
    

    Output

    clean the apple
    clean the pear
    clean the banana
    cut the apple
    cut the pear
    cut the banana
    eat the apple
    eat the pear
    eat the banana