How to generate all combination from values in dict of lists in Python

If you want to keep the key:value in the permutations you can use: import itertools keys, values = zip(*my_dict.items()) permutations_dicts = [dict(zip(keys, v)) for v in itertools.product(*values)] this will provide you a list of dicts with the permutations: print(permutations_dicts) [{‘A’:’D’, ‘B’:’F’, ‘C’:’I’}, {‘A’:’D’, ‘B’:’F’, ‘C’:’J’}, … ] Disclaimer: not exactly what the OP was asking, … Read more

How to create all possible pair combinations without duplicates in Google Sheets?

google-sheets It is a hard task for native functions. Try a script and use it as a custom function: function getTournament(teams_from_range) { // teams_from_range — 2D Array var teams = []; // convert to list teams_from_range.forEach(function(row) { row.forEach(function(cell) { teams.push(cell); } ); } ); return getTournament_(teams); } function getTournament_(teams) { var start = 0; var … Read more

Postgresql enforce unique two-way combination of columns

A variation on Neil’s solution which doesn’t need an extension is: create table friendz ( from_id int, to_id int ); create unique index ifriendz on friendz(greatest(from_id,to_id), least(from_id,to_id)); Neil’s solution lets you use an arbitrary number of columns though. We’re both relying on using expressions to build the index which is documented https://www.postgresql.org/docs/current/indexes-expressional.html