Nick Zulanas Nick Zulanas - 2 months ago 6
Python Question

Separating/accessing a triple tuple list like [[[x,y],z],p]?

I am trying to access variables stored in a triple tuple list in python and I am not sure how to do it. I would like to be able to go through the list in a for loop and get x,y,x, & p from each tuple. How would I do that?

MovesList = [ [[[1,2],3],1] , [[[2,5],3],1] , [[[1,3],0],2] ]

Answer

You can unpack tuples as you iterate over them:

Python 2:

>>> for ((x,y),z),p in MovesList:
...     print x, y, z, p

Python 3:

>>> for ((x,y),z),p in MovesList:
...     print(x,y,z,p)

Both of which result in:

1 2 3 1
2 5 3 1
1 3 0 2
Comments