duhaime
duhaime

Reputation: 27594

Python: "Distributing" strings from two lists into one list

I'm sorry to ask such a simple question, but is there a Python method that will allow one to "distribute" the strings in two lists into a single list? I mean "distribute" in the mathematical sense, so distributing a list ("a", "b") across another list ("c", "d") and rendering the output as a single list would yield ("a", "c", "a", "d", "b", "c", "b", "d"). I tried looking on SO for such a method, but haven't found anything yet.

It wouldn't be to hard to write out a script that accomplishes this, but is there a native method or a method from any extant package that can accomplish this kind of "distribution" for strings? (I ask because I'm trying to run a series of proximity searches through an interface, and I would like to find all records in the database that contain one or more words from two distinct lists of words.)

Upvotes: 0

Views: 404

Answers (1)

dstromberg
dstromberg

Reputation: 7177

From here it's a simple matter of flattening the containers down to one:

>>> print(list(itertools.product(['a', 'b'], ['c', 'd'])))
[('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd')]

Upvotes: 4

Related Questions