1

May be its an absurd question but I have this code:

from itertools import product

variable_ = 'variable_'
for i,oid, val in product(xrange(0,7), varBinds):
    variable_ += `i`
    print('%s = %s' % (variable_,val.prettyPrint()))

I get the error ValueError: need more than 2 values to unpack.I don't want to use oid for now but may be later in the same loop I'll require it. I searched for the error and possible solutions but none worked.

All i want to do is for i in range(0,7) && for oid, val in varBinds, and get expected data as

variable_0=<some value in varBinds>
variable_1=<some other value in varBinds>
.
.
variable_6=<last value in varBinds>

how can i do that?

UPDATE:

This is what i did:

for i, (oid, val) in enumerate(varBinds):
variable_ += `i`
    print('%s = %s' % (variable_,val.prettyPrint()))
    variable_="variable_"

Worked Perfectly. Thanks @RemcoGerlich for the help

Anukruti
  • 87
  • 1
  • 9

1 Answers1

1

Just note that the two latter values are in a tuple themselves:

for i, (oid, val) in product(xrange(0,7), varBinds):

Edit: from your example, you don't want product at all, but enumerate:

for i, (oid, val) in enumerate(varBinds):

See What does enumerate mean? .

Or possibly itertools.izip (an iterator version of zip), if the xrange was just an example:

from itertools import izip

for i, (old, val) in izip(xrange(0, 7), varBinds):
Community
  • 1
  • 1
RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79
  • Thanks for the quick reply @RemcoGerlich ! I tried this. Worked some what like i want. But I am not getting my perfect output.. May be I am horrible at loop concepts lol. – Anukruti Mar 16 '17 at 09:54
  • Thanks a lot!!! I could at least get this result `variable_0 = ` `variable_01 = ` `......variable_0123456 = ` Now will check what is my fault.. Thanks again!! :) – Anukruti Mar 16 '17 at 10:06