Ask Your Question
4

What is the process to generate a list consisting of two elements from one list for every element in another list?

asked 2021-11-17 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-01-25 04:00:00 +0000

scrum gravatar image

The process to generate a list consisting of two elements from one list for every element in another list is called cartesian product or cross product.

Here are the steps:

  1. Create two lists: List A and List B.

  2. Iterate through every element in List A.

  3. For each element in List A, iterate through every element in List B.

  4. For each element in List B, create a new list that contains the element from List A and the element from List B.

  5. Add the new list to a result list.

  6. After iterating through all elements in List A, the result list will contain every possible combination of two elements from List B for each element in List A.

Example:

List A: [1, 2, 3]

List B: [4, 5]

Result list: [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-11-17 11:00:00 +0000

Seen: 11 times

Last updated: Jan 25 '23