Python - Tuple Tuples unpacking

Introduction

Both tuples and named tuples support unpacking tuple assignment.

Demo

Rec = namedtuple('Rec', ['name', 'age', 'jobs'])       # Make a generated class 
bob = Rec('Bob', 40.5, ['dev', 'mgr'])    # For both tuples and named tuples 
name, age, jobs = bob                     # Tuple assignment (Chapter 11) 
print( name, jobs )

for x in bob: print(x)                    # Iteration context (Chapters 14, 20)
# w w w .java 2  s  .  c  o  m

Result

Dictionary unpacking

Demo

bob = {'name': 'Bob', 'age': 40.5, 'jobs': ['dev', 'mgr']} 
job, name, age = bob.values() # from  www .j a  va 2s . c  om
print( name, job )                        # Dict equivalent, but order may vary 

for x in bob: print(bob[x])               # Step though keys, index values 
for x in bob.values(): print(x)           # Step through values view

Result