4
>>> import itertools
>>> n = [1,2,3,4]
>>> combObj = itertools.combinations(n,3)
>>>
>>> combObj
<itertools.combinations object at 0x00000000028C91D8>
>>>
>>> list(combObj)
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
>>>
>>> for i in list(combObj): #This prints nothing
...     print(i)
...
  1. How can i iterate through combObj ?

  2. How can i convert
    [(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
    to
    [[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]

3 Answers 3

7

Once you iterate through the itertools.combinations object once, it's been used up and you can't iterate over it a second time.

If you need to reuse it, the proper way is to make it a list or tuple as you did. All you need to do is give it a name (assign it to a variable) so it sticks around.

combList = list(combObject) # Don't iterate over it before you do this!

If you want to iterate over it just once, you just don't call list on it at all:

for i in combObj: # Don't call `list` on it before you do this!
    print(i)

Side note: The standard way to name object instances / normal variables would be comb_obj rather than combObj. See PEP-8 for more info.

To convert the inner tuples to lists, use a list comprehension and the list() built-in:

comb_list = [(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]
comb_list = [list(item) for item in comb_list]
Sign up to request clarification or add additional context in comments.

2 Comments

Got it! I didnt know you can't iterate over it a second time. Thanks! About PEP8, i have read about it, but it feels hard for me to change the way i name variables, functions,etc.. I dont know, it just doesnt feel right to change a style every time i work in a different language
If you're just coding for yourself, then it's not a problem to use whatever style you're comfortable with. But if your submitting code to any larger community, professional or not, you'll generally need to adhere to their style rules. Python just attempts to unify all its communities under a single style -- they're reducing the number of styles you need to know, not increasing it. I wish I could always code in my own style too, it's just not possible.
0

To convert to a list of lists, you can do:

[list(item) for item in combObj]

2 Comments

or list(map(list, combObj))
@Austin, map doesn't return a list for Python3. Note the Python-3.x tag
0

generators are good because they don't use much memory. If you're using it a lot and have memory then save it as a tuple instead of a generator.

Otherwise I often make a function to return the generator each time I want to use it:

>>> def get_combObj():
...     return itertools.combinations(n,3)
...
>>> for i in get_combObj():
...     print list(i)
...
[1, 2, 3]
[1, 2, 4]
[1, 3, 4]
[2, 3, 4]

(you can call get_combObj() as much as you want)

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.