0

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.)

1 Answer 1

4

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')]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.