View Problem

Produce the combinations from two lists

Given two lists, produce the list of tuples formed by taking the combinations from the individual lists. E.g. given the letters ["a", "b", "c"] and the numbers [4, 5], produce the list: [["a", 4], ["b", 4], ["c", 4], ["a", 5], ["b", 5], ["c", 5]]
DiskEdit
python 2.5.2
[(x, y) for y in [1,2] for x in ['a','b','c']]
DiskEdit
python >= 2.6
import itertools
[x for x in itertools.product(["a", "b", "c"], [4, 5])]
DiskEdit
clojure
(defn combine [lst1 lst2]
(mapcat (fn [x] (map #(list % x) lst1)) lst2))
DiskEdit
clojure
(mapcat (fn [x] (map #(list % x) ["a", "b", "c"])) [4, 5])
ExpandDiskEdit
fantom
[4,5].each |Int i| { ["a","b","c"].each |Str s| { r.add([i,s]) } }
ExpandDiskEdit
cpp C++/CLI .NET 2.0
Specialized::StringCollection^ combinations = gcnew Specialized::StringCollection;

for each(int number in numbers)
for each(String^ letter in letters)
combinations->Add(makeCombo(letter, number));
ExpandDiskEdit
cpp
string letters[] = { "a", "b", "c" };
int numbers[] = { 4, 5 };
list<pair<string,int> > combo;

for (int n = 0; n < sizeof numbers / sizeof *numbers; n++)
for (int l = 0; l < sizeof letters / sizeof *letters; l++)
combo.push_back(make_pair(letters[l], numbers[n]));

cout << combo << endl;

Submit a new solution for python, clojure, fantom, or cpp
There are 19 other solutions in additional languages (csharp, erlang, fsharp, groovy ...)