1

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}
1
  • lots of great answers :) Commented Jan 31, 2012 at 4:23

3 Answers 3

5

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}
Sign up to request clarification or add additional context in comments.

2 Comments

What should happen if one of the lists is empty (with itertools)?
@ofko: Exactly the same thing as if one of the lists is empty in the nested loop version: nothing. If you iterate over an empty list, you never get into the inner code.
2

Just use a for-loop:

names = ['tom','marry','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}

But, if you ask me, I always think everything looks better with .format():

for name in names:
    for fruit in fruits:
        for hobby in hobbies:
            print '{} likes to eat {} while {}'.format(name, fruit, hobby)

1 Comment

I don't think this is correct -- he wants all possible combinations (4*4*4 = 64).
2

The itertools module provides the product function, that yields all posible tuples:

>>> from itertools import product
>>> names = ['tom','marry','jessica','john']
>>> fruits = ['oranges','apples','grapes','bananas']
>>> hobbies = ['dancing','sitting','bicycling','watching tv']
>>> for n, f, h in product(names, fruits, hobbies):
...     print '%s likes to eat %s while %s' % (n, f, h)

You could also use the tuple directly:

>>> for t in product(names, fruits, hobbies):
...     print '%s likes to eat %s while %s' % t

1 Comment

the second one is so concise, I love python!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.