list of all possible values

Andreas Tawn andreas.tawn at
Mon Jul 13 12:51:07 EDT 2009

> David Gibb:
> > For example: if my values are ['a', 'b', 'c'], then all possible
> > of length 2 would be: aa, ab, ac, ba, bb, bc, ca, cb, cc.
> >>> from itertools import product
> >>> list(product("abc", repeat=2))
> [('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b',
> 'c'), ('c', 'a'), ('c', 'b'), ('c', 'c')]
> Bye,
> bearophile

Certainly possible with list comprehensions.

>>> a = "abc"
>>> [(x, y) for x in a for y in a]
[('a', 'a'), ('a', 'b'), ('a', 'c'), ('b', 'a'), ('b', 'b'), ('b', 'c'),
('c', 'a'), ('c', 'b'), ('c', 'c')]

But I like bearophile's version better.



More information about the Python-list mailing list