Given:
date_range = ['2022-05-27','2022-05-28']
hour_range = [0,2,4,6]
I want to generate:
[
('2022-05-27',0),
('2022-05-27',2),
('2022-05-27',4),
('2022-05-27',6),
('2022-05-28',0),
('2022-05-28',2),
('2022-05-28',4),
('2022-05-28',6),
]
I tried zip(), map(), etc. which doesnt exactly get the expected output. What built-in function can I use to avoid writing multiple loops?
>Solution :
You want itertools.product():
from itertools import product
list(product(date_range, hour_range))
This outputs:
[
('2022-05-27', 0), ('2022-05-27', 2), ('2022-05-27', 4), ('2022-05-27', 6),
('2022-05-28', 0), ('2022-05-28', 2), ('2022-05-28', 4), ('2022-05-28', 6)
]