Python string formating using values from multiple lists
Question: I'm trying to format a string using values from several lists. The following
is pseudo-code but should give an idea of the expected output. The output
would be combination of each item in each list: **each person likes to eat all
fruits while doing all hobbies**. So how to do this in python?

There should be `len(names)*len(fruits)*len(hobbies)` possibilities (64 in my
example)

    
    
    names = ['tom','marry','jessica','john']
    fruits = ['oranges','apples','grapes','bananas']
    hobbies = ['dancing','sitting','bicycling','watching tv']
    
    print '%(name)s likes to eat %(fruit)s while %(hobby)s \n'
           % {'name':names, 'fruit':fruits, 'hobby':hobbies}
    


Answer: If I understand your "The output would be combination of each item in each
list: each person likes all fruits while doing each hobby" line, you want
every possible combination. You can do this in a nested loop way:

    
    
    names = ['tom','mary','jessica','john']
    fruits = ['oranges','apples','grapes','bananas']
    hobbies = ['dancing','sitting','bicycling','watching tv']
    
    for name in names:
        for fruit in fruits:
            for hobby in hobbies:
                print '%(name)s likes to eat %(fruit)s while %(hobby)s' % {'name':name, 'fruit':fruit, 'hobby':hobby}
    

which produces

    
    
    tom likes to eat oranges while dancing
    tom likes to eat oranges while sitting
    tom likes to eat oranges while bicycling
    tom likes to eat oranges while watching tv
    tom likes to eat apples while dancing
    [etc.]
    john likes to eat bananas while bicycling
    john likes to eat bananas while watching tv
    

or you could use the itertools module, which has a function `product` which
gives you every possible combination of the input lists:

    
    
    import itertools
    
    for name, fruit, hobby in itertools.product(names, fruits, hobbies):
        print '%(name)s likes to eat %(fruit)s while %(hobby)s' % {'name':name, 'fruit':fruit, 'hobby':hobby}
    

