itertools.product - return list instead of tuple

mchangun

I want itertools.product to return a list instead of a tuple. I am currently doing it by creating my own function like this:

def product_list(*args, **kwds):
    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
    pools = map(tuple, args) * kwds.get('repeat', 1)
    result = [[]]
    for pool in pools:
        result = [x + [y] for x in result for y in pool]
    for prod in result:
        yield list(prod)  # Yields list() instead of tuple()

The code is from the Python docs - I just modified the last line. This works fine but doesn't seem very clever.

What are the other ways of doing this? I'm thinking of using something like a decorator or wrapping it with my own generator function. I'm not too familiar with either concepts so would appreciate if someone can show me.

Edit I'm doing something messy like this:

for r0 in product_list([0, 1], repeat=3):
    r0.insert(0, 0)
    for r1 in product_list([0, 1], repeat=3):
        r1.insert(1, 0)
        for r2 in product_list([0, 1], repeat=3):
            r2.insert(2, 0)
            for r3 in product_list([0, 1], repeat=3):
                r3.insert(3, 0)

so I would prefer my function return a list instead of having to cast it every time. (I know the code is messy and needs recursion but I will think about that later. I'm more interested to learn how to do what I described above)

Blckknght

itertools.product is a generator, and you can easily chain generators together. Here's a generator expression that changes each tuple yielded by product into a list:

(list(tup) for tup in itertools.product(iterable1, iterable2, etc))

In your example code, you could use the generator expression, or you could use a different approach to add an extra value on to the front of your values while keeping them as tuples:

for r0 in itertools.product([0, 1], repeat=3):
    r0 = (0,) + r0 # keep r0 a tuple!
    for r1 in itertools.product([0, 1], repeat=3):
        r1 = (1,) + r1 # same here
        # ...

Since you don't show what you're using your rN variables for, it's impossible to give you a definitive answer as to what would be the best way to go. (It is a bit of a code smell that you have numbered variables.) Indeed, since your loops are simply computing three more 0 or 1 digits, you might be able to get away with a single product call, that generates an list of n different r values in one go:

for bits in itertools.product([0, 1], repeat=3*n):
    rs = [(i,) + bits[3*i:3*i+3] for i in range(n)]
    # do something with the list of r tuples here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Itertools product of list

From Dev

How to return back a list instead of tuple in psycopg2

From Dev

Python tuple instead of list

From Dev

Python itertools: Best way to unpack product of product of list of lists

From Dev

Using itertools permutations and product to return all possible character orderings

From Dev

Return multiple vars: list/tuple

From Dev

Return a list instead of IQueryable

From Dev

Return a list of lines as list of tuple with formatting the value with $

From Dev

Using list instead of tuple in module __all__

From Dev

Product of tuple

From Dev

Itertools.permutations returns <object> instead of list of permutations

From Dev

Python itertools product, but conditional?

From Dev

Return dictionary value as tuple or list from a string

From Dev

Return dictionary value as tuple or list from a string

From Dev

In a list of tuples, return tuple[1] if tuple[0] is a duplicate of another tuple[0] in the list

From Dev

Is there a way to call ExecuteQuery on a SQL DataContext and return a Tuple instead of a class/object

From Dev

HQL return List<MyClass> instead List<Object[]>

From Dev

Numpy equivalent of itertools.product

From Dev

Minimum of itertools.product in python

From Dev

Itertools product without repeating duplicates

From Dev

Comparing id of itertools.product

From Dev

Merging the results of itertools.product?

From Dev

Python itertools product of undefinite dimension

From Dev

List of tuple<double,double>. Comma instead of point in double values

From Dev

Why does unpacking give a list instead of a tuple in Python?

From Dev

List of tuple<double,double>. Comma instead of point in double values

From Dev

Appending a tuple to a np array returns a list of scalars instead of tuples

From Dev

Python-docx cells returning tuple instead of list

From Dev

Return an RDD from takeOrdered, instead of a list

Related Related

  1. 1

    Itertools product of list

  2. 2

    How to return back a list instead of tuple in psycopg2

  3. 3

    Python tuple instead of list

  4. 4

    Python itertools: Best way to unpack product of product of list of lists

  5. 5

    Using itertools permutations and product to return all possible character orderings

  6. 6

    Return multiple vars: list/tuple

  7. 7

    Return a list instead of IQueryable

  8. 8

    Return a list of lines as list of tuple with formatting the value with $

  9. 9

    Using list instead of tuple in module __all__

  10. 10

    Product of tuple

  11. 11

    Itertools.permutations returns <object> instead of list of permutations

  12. 12

    Python itertools product, but conditional?

  13. 13

    Return dictionary value as tuple or list from a string

  14. 14

    Return dictionary value as tuple or list from a string

  15. 15

    In a list of tuples, return tuple[1] if tuple[0] is a duplicate of another tuple[0] in the list

  16. 16

    Is there a way to call ExecuteQuery on a SQL DataContext and return a Tuple instead of a class/object

  17. 17

    HQL return List<MyClass> instead List<Object[]>

  18. 18

    Numpy equivalent of itertools.product

  19. 19

    Minimum of itertools.product in python

  20. 20

    Itertools product without repeating duplicates

  21. 21

    Comparing id of itertools.product

  22. 22

    Merging the results of itertools.product?

  23. 23

    Python itertools product of undefinite dimension

  24. 24

    List of tuple<double,double>. Comma instead of point in double values

  25. 25

    Why does unpacking give a list instead of a tuple in Python?

  26. 26

    List of tuple<double,double>. Comma instead of point in double values

  27. 27

    Appending a tuple to a np array returns a list of scalars instead of tuples

  28. 28

    Python-docx cells returning tuple instead of list

  29. 29

    Return an RDD from takeOrdered, instead of a list

HotTag

Archive