views:

142

answers:

5

I've got a python function that should loop through a tuple of coordinates and print their contents:

def do(coordList):
    for element in coordList:
        print element
y=((5,5),(4,4))
x=((5,5))

When y is run through the function, it outputs (5,5) and (4,4), the desired result. However, running x through the function outputs 5 and 5.

Is there a way to force x to be defined as a tuple within a tuple, and if not, what is the easiest way to resolve this problem?

+8  A: 

Use a trailing comma for singleton tuples.

x = ((5, 5),)
sykora
+6  A: 
x=((5,5),)

(x) is an expression (x,) is a singleton tuple.

S.Lott
+3  A: 

This is an old and infuriating quirk of python syntax. You have to include a trailing comma to make Python see a tuple:

x = ((5,5),)
Jonathan Feinberg
+2  A: 

You need to add a comma after your first tuple. ((5,5),) should work.

Sean Vieira
+1  A: 

Simply add a comma:

x=((5,5),)
Ranieri