1

I want to iterate over the elements ((a,b),(x,y)) so I tried:

def method(tuple): ((a,b),(x,y))= tuple for element in tuple: ..... 

But then I read another stackoverflow page which suggested something like this:

def method(tuple): ((a,b),(x,y))= tuple for element in tuple[0:4]: ..... 

Both resulted in the error: ValueError: need more than 1 value to unpack.

Is this action not allowed in python, or do I just have a syntax problem? I have checked the python docs as well.

Thanks for any advice.

Edit

map = ((1,0),(3,2)) def count(map): ((a,b),(x,y))= tuple inc=0 for element in tuple: inc+=1 
3
  • 1
    Please show us your actual code and the data you used. Commented Feb 23, 2014 at 7:06
  • 2
    It's not clear what you're trying to do. After you unpack the tuple, why do you need to iterate? HOWEVER I don't think that's your problem. The error "ValueError: need more than 1 value to unpack." indicates that the tuple doesn't have form you expect. It's trying to break it up but failing. I'd make sure the data's actually coming in the form you're expecting (a log statement?) Commented Feb 23, 2014 at 7:18
  • I want to access each element to see if I could count how many elements there were in the tuple. But you're right. I tried shifting some breakpoints around and running the debugger and found that map was not in the form I thought it was. Commented Feb 23, 2014 at 7:28

1 Answer 1

8

If you have a tuple of tuples, of the form ((a, b), (x, y)), you can iterate over its elements:

def method(tuples): for tup in tuples: for e in tup: print e 

If you want to have 4 variables, you can use them separately:

def method(tuples): (a, b), (x, y) = tuples print a, b, x, y 

Note: Don't use Python built-in names as name of variables. In other words, don't use tupleas a name of a variable because it's a type in Python. Use something else, like tuples, my_tuple, ...

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.