I sent this 17hrs ago but I guess it just went through. Apologies for the 
redundant comments... ---- On Tue, 01 Mar 2022 18:57:02 -0600  
om+pyt...@omajoshi.com  wrote ----For completeness, the itertools solution 
(which returns an iterator) is

>>> os = ["Linux","Windows"]
>>> region = ["us-east-1", "us-east-2"]
>>> import itertools
>>> itertools.product(os,region)
<itertools.product object at 0x0000011C543D3340>
>>> list(itertools.product(os,region))
[('Linux', 'us-east-1'), ('Linux', 'us-east-2'), ('Windows', 'us-east-1'), 
('Windows', 'us-east-2')]

There are probably use cases where you want the iterator and others where you 
want the list comprehension. I think the itertools looks nice and it's easier 
to generalize it to N=3,4,5,... lists than writing out `for a in list1 for b in 
list2 for c in list3` etc -- also, you can do things like 
itertools.product(*list_of_lists) to get the product if you have a variable 
number of lists. Not exactly sure about speed comparison but my instinct is 
that a double/triple list comprehension is going to be slower than letting 
itertools do its magic.


 ---- On Tue, 01 Mar 2022 18:20:16 -0600  <2qdxy4rzwzuui...@potatochowder.com> 
wrote ----
 > On 2022-03-01 at 19:12:10 -0500,
 > Larry Martell <larry.mart...@gmail.com> wrote:
 > 
 > > If I have 2 lists, e.g.:
 > > 
 > > os = ["Linux","Windows"]
 > > region = ["us-east-1", "us-east-2"]
 > > 
 > > How can I get a list of tuples with all possible permutations?
 > > 
 > > So for this example I'd want:
 > > 
 > > [("Linux", "us-east-1"), ("Linux", "us-east-2"), ("Windows",
 > > "us-east-1"), "Windows", "us-east-2')]
 > > 
 > > The lists can be different lengths or can be 0 length. Tried a few
 > > different things with itertools but have not got just what I need.
 > 
 > [(o, r) for o in os for r in region]
 > 
 > Feel free to import itertools, but it's not really necessary.  ;-)
 > -- 
 > https://mail.python.org/mailman/listinfo/python-list
 > 
-- 
https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to