Cartesian product of a dictionary of lists in python

Cartesian product of a dictionary of lists in python

You can compute the Cartesian product of a dictionary of lists in Python using itertools.product. Here's an example:

from itertools import product

# Define a dictionary of lists
data = {
    'A': [1, 2],
    'B': [3, 4],
    'C': [5, 6]
}

# Compute the Cartesian product
result = [dict(zip(data.keys(), values)) for values in product(*data.values())]

# Print the result
for item in result:
    print(item)

In this code:

  1. We import itertools.product to compute the Cartesian product.

  2. We define a dictionary data with lists as values. Each list represents the possible values for a key.

  3. We use product(*data.values()) to compute the Cartesian product of the values in the dictionary. This results in an iterable of tuples containing the possible combinations of values.

  4. We use a list comprehension to convert each tuple into a dictionary where keys are the original keys from the dictionary and values are the corresponding values from the tuple.

  5. Finally, we print each item in the result, which will display all possible combinations of values from the dictionary.

Examples

  1. "Python code for Cartesian product of dictionary lists" Description: This code generates the Cartesian product of lists stored in a dictionary in Python, producing all possible combinations of elements from the lists.

    from itertools import product
    
    def cartesian_product(dictionary):
        keys = dictionary.keys()
        values = dictionary.values()
        return [dict(zip(keys, combination)) for combination in product(*values)]
    
    # Example usage:
    my_dict = {'A': [1, 2], 'B': ['a', 'b']}
    result = cartesian_product(my_dict)
    print(result)
    
  2. "Python Cartesian product of dictionary values" Description: This Python code snippet demonstrates how to compute the Cartesian product of values stored in a dictionary.

    from itertools import product
    
    def cartesian_product(dictionary):
        values = dictionary.values()
        return list(product(*values))
    
    # Example usage:
    my_dict = {'A': [1, 2], 'B': ['a', 'b']}
    result = cartesian_product(my_dict)
    print(result)
    
  3. "How to find Cartesian product of dictionary lists in Python" Description: Here's a Python function to find the Cartesian product of lists stored in a dictionary.

    from itertools import product
    
    def cartesian_product(dictionary):
        return list(product(*dictionary.values()))
    
    # Example usage:
    my_dict = {'A': [1, 2], 'B': ['a', 'b']}
    result = cartesian_product(my_dict)
    print(result)
    
  4. "Python code to compute Cartesian product of dictionary values" Description: This Python code calculates the Cartesian product of values stored in a dictionary using itertools.product.

    from itertools import product
    
    def cartesian_product(dictionary):
        return list(product(*dictionary.values()))
    
    # Example usage:
    my_dict = {'A': [1, 2], 'B': ['a', 'b']}
    result = cartesian_product(my_dict)
    print(result)
    
  5. "Python Cartesian product of dictionary of lists" Description: Below is a Python implementation to compute the Cartesian product of lists stored in a dictionary.

    from itertools import product
    
    def cartesian_product(dictionary):
        return list(product(*dictionary.values()))
    
    # Example usage:
    my_dict = {'A': [1, 2], 'B': ['a', 'b']}
    result = cartesian_product(my_dict)
    print(result)
    
  6. "How to find all combinations of dictionary lists in Python" Description: Here's a Python function to find all combinations of lists stored in a dictionary, computing their Cartesian product.

    from itertools import product
    
    def cartesian_product(dictionary):
        return list(product(*dictionary.values()))
    
    # Example usage:
    my_dict = {'A': [1, 2], 'B': ['a', 'b']}
    result = cartesian_product(my_dict)
    print(result)
    
  7. "Python code to get Cartesian product of dictionary keys and values" Description: This Python code snippet demonstrates how to obtain the Cartesian product of keys and values stored in a dictionary.

    from itertools import product
    
    def cartesian_product(dictionary):
        return list(product(dictionary.keys(), *dictionary.values()))
    
    # Example usage:
    my_dict = {'A': [1, 2], 'B': ['a', 'b']}
    result = cartesian_product(my_dict)
    print(result)
    
  8. "Python Cartesian product of dictionary items" Description: Here's a Python function to compute the Cartesian product of items stored in a dictionary.

    from itertools import product
    
    def cartesian_product(dictionary):
        return list(product(*dictionary.items()))
    
    # Example usage:
    my_dict = {'A': [1, 2], 'B': ['a', 'b']}
    result = cartesian_product(my_dict)
    print(result)
    
  9. "How to calculate Cartesian product of dictionary values in Python" Description: This Python code calculates the Cartesian product of values stored in a dictionary, producing all possible combinations.

    from itertools import product
    
    def cartesian_product(dictionary):
        return list(product(*dictionary.values()))
    
    # Example usage:
    my_dict = {'A': [1, 2], 'B': ['a', 'b']}
    result = cartesian_product(my_dict)
    print(result)
    
  10. "Python code for finding Cartesian product of dictionary of lists" Description: Below is a Python implementation to find the Cartesian product of lists stored in a dictionary.

    from itertools import product
    
    def cartesian_product(dictionary):
        return list(product(*dictionary.values()))
    
    # Example usage:
    my_dict = {'A': [1, 2], 'B': ['a', 'b']}
    result = cartesian_product(my_dict)
    print(result)
    

More Tags

titlebar tui contact-form electron-packager bluebird prettier relational-database git-archive ion-select fencepost

More Python Questions

More Various Measurements Units Calculators

More Housing Building Calculators

More Dog Calculators

More Transportation Calculators