1

I have two lists which I want to zip

List A:

["hello ", "world "]

List B:

["one", "two", "three"]

I want to zip the elements in the lists like so:

[("hello","one")
("hello","two")
("hello","three")
("world","one")
("world","two")
("world","three")]

Obviously, I can use a double for loop and append the elements but I am wondering what would be a good pythonie way of doing this?

Georgi Angelov
  • 4,338
  • 12
  • 67
  • 96

1 Answers1

5

This seems like a perfect use-case for itertools.product

>>> import itertools
>>> list(itertools.product(['hello', 'world'], ['one', 'two', 'three']))
[('hello', 'one'), ('hello', 'two'), ('hello', 'three'), ('world', 'one'), ('world', 'two'), ('world', 'three')]
mgilson
  • 300,191
  • 65
  • 633
  • 696