Get all combinations of elements from two lists?


List

if i have two lists

l1 = [ 'A', 'B' ]

l2 = [ 1, 2 ]

What is the most elegant way to get a pandas dataframe like this:

+-----+-----+-----+
|     | l1  | l2  |
+-----+-----+-----+
|  0  | A   | 1   |
+-----+-----+-----+
|  1  | A   | 2   |
+-----+-----+-----+
|  2  | B   | 1   |
+-----+-----+-----+
|  3  | B   | 2   |
+-----+-----+-----+

Note that the first column is the index.

Bezard Nouri

Used for :productitertools

>>> from itertools import product
>>> pd.DataFrame(list(product(l1, l2)), columns=['l1', 'l2'])
  l1  l2
0  A   1
1  A   2
2  B   1
3  B   2

Related


Get all combinations of elements from two lists?

List if i have two lists l1 = [ 'A', 'B' ] l2 = [ 1, 2 ] What is the most elegant way to get a pandas dataframe like this: +-----+-----+-----+ | | l1 | l2 | +-----+-----+-----+ | 0 | A | 1 | +-----+-----+-----+ | 1 | A | 2 | +-----+-----+--

How to get all combinations from multiple lists?

GhostKU I'm not sure my question is correct, but I don't know how to explain it. So I have something like a = ['11', '12'] b = ['21', '22'] c = ['31', '32'] And I need to get something like: result = [ ['11', '21', '31'], ['11', '21', '32'], ['11'

all possible combinations of two lists

wei Given I have two lists: val ints = listOf(0, 1, 2) val strings = listOf("a", "b", "c") I want all possible combinations of their elements 0a, 1a, 2a, 0betc Is there a more elegant way than: ints.forEach { int -> strings.forEach { string ->

How to get all combinations from multiple lists?

GhostKU I'm not sure my question is correct, but I don't know how to explain it. So I have something like a = ['11', '12'] b = ['21', '22'] c = ['31', '32'] And I need to get something like: result = [ ['11', '21', '31'], ['11', '21', '32'], ['11'

How to get all combinations from multiple lists?

GhostKU I'm not sure my question is correct, but I don't know how to explain it. So I have something like a = ['11', '12'] b = ['21', '22'] c = ['31', '32'] And I need to get something like: result = [ ['11', '21', '31'], ['11', '21', '32'], ['11'

all possible combinations of two lists

wei Given I have two lists: val ints = listOf(0, 1, 2) val strings = listOf("a", "b", "c") I want all possible combinations of their elements 0a, 1a, 2a, 0betc Is there a more elegant way than: ints.forEach { int -> strings.forEach { string ->

Get all combinations of two elements with distance 1

Peaceful Kegan Liberation I'm really sorry for the title, but I don't know how to describe my problem in words. So here is the example: Assuming we have a string "123", my function should yield: 1 2 3 12 3 1 23 or for the string "1234": 1 2 3 4 12 3 4 12 34 1

R - all pairwise combinations of elements in two lists

username I'm pretty sure there's an easy answer to this, but due to my limited R experience I'm having a hard time solving it. I have a list of dataframes representing different experiments, and for each of these dataframes I generate a regression model - the

Find all combinations of two lists

Fear Robert I have a question about python. I have a grid like the attached image. I have coordinates for all states. The coordinates of the states are shown in the A and B lists: A = [1,2,3] B = [4,5,6] I want to find the coordinates of the state I want. For

Get all combinations of elements from two lists?

List if i have two lists l1 = [ 'A', 'B' ] l2 = [ 1, 2 ] What is the most elegant way to get a pandas dataframe like this: +-----+-----+-----+ | | l1 | l2 | +-----+-----+-----+ | 0 | A | 1 | +-----+-----+-----+ | 1 | A | 2 | +-----+-----+--

Get all combinations of elements from two lists?

List if i have two lists l1 = [ 'A', 'B' ] l2 = [ 1, 2 ] What is the most elegant way to get a pandas dataframe like this: +-----+-----+-----+ | | l1 | l2 | +-----+-----+-----+ | 0 | A | 1 | +-----+-----+-----+ | 1 | A | 2 | +-----+-----+--

How to get all combinations from multiple lists?

GhostKU I'm not sure my question is correct, but I don't know how to explain it. So I have something like a = ['11', '12'] b = ['21', '22'] c = ['31', '32'] And I need to get something like: result = [ ['11', '21', '31'], ['11', '21', '32'], ['11'

all possible combinations of two lists

wei Given I have two lists: val ints = listOf(0, 1, 2) val strings = listOf("a", "b", "c") I want all possible combinations of their elements 0a, 1a, 2a, 0band many more Is there a more elegant way than: ints.forEach { int -> strings.forEach { string ->

all possible combinations of two lists

wei Given I have two lists: val ints = listOf(0, 1, 2) val strings = listOf("a", "b", "c") I want all possible combinations of their elements 0a, 1a, 2a, 0band many more Is there a more elegant way than: ints.forEach { int -> strings.forEach { string ->

Get all combinations of two elements with distance 1

Peaceful Kegan Liberation I'm really sorry for the title, but I don't know how to describe my problem in words. So here is the example: Assuming we have a string "123", my function should yield: 1 2 3 12 3 1 23 or for the string "1234": 1 2 3 4 12 3 4 12 34 1

R - all pairwise combinations of elements in two lists

username I'm pretty sure there's an easy answer to this, but due to my limited R experience I'm having a hard time solving it. I have a list of dataframes representing different experiments, and for each of these dataframes I generate a regression model - the