named tuple python iterate

Solutions on MaxInterview for named tuple python iterate by the best coders in the world

showing results for - "named tuple python iterate"
Ivanna
17 Nov 2016
1>>> a = A(1, 2)
2>>> for name, value in a._asdict().iteritems():
3    print name
4    print value
5
6
7a
81
9b
102
11
12>>> for fld in a._fields:
13    print fld
14    print getattr(a, fld)
15
16
17a
181
19b
202
21
Garth
16 Feb 2020
1You could google on "tuple unpacking". This can be used in various places in Python. The simplest is in assignment
2
3>>> x = (1,2)
4>>> a, b = x
5>>> a
61
7>>> b
82
9In a for loop it works similarly. If each element of the iterable is a tuple, then you can specify two variables and each element in the loop will be unpacked to the two.
10
11>>> x = [(1,2), (3,4), (5,6)]
12>>> for item in x:
13...     print "A tuple", item
14A tuple (1, 2)
15A tuple (3, 4)
16A tuple (5, 6)
17>>> for a, b in x:
18...     print "First", a, "then", b
19First 1 then 2
20First 3 then 4
21First 5 then 6
22The enumerate function creates an iterable of tuples, so it can be used this way.
queries leading to this page
how to iterate tuple in pythoniterate though tupleiterate through a tuple pythoncan i iterate througha tuple pythoniterate through a tuplefor in tuple pythonhow to iterate over the tuplehow to iterate over tuple in pythonpython for loop tuplepython loop through a tuplepython loop through tuplepython iterate tuple listiterate tuple in pythoncan you iterate through a tuple pythonitterate through named tuple in pythoniterate through tupeles in pythonpython iterate items in a tuplecan you loop through a tuple in pythonhow to iterate in tuple pythonloop over a tuple pythoniterate tupleshow to iterate for loop with a tupleloop iteration tuples pythonnamed tuple python iterateloop on tuple pythonhow to loop through tuple in pythoniterate over a tuple in pythoniterate items in tuple pythonpython iterate a tuplefor loop tuple pythonpython loop tupleiterate tuples pythonpython iterate through tupleiterate over tupleiterate tuple with key pythoniterating tuple in pythoniterate through tuple python 5ctuple iterate pythonloop through tuple pythoniterate over tuple pythonhow to traverse through a tuple in pythonhow to iterate tuples in pythonfor loop in tuple pythonfor loop in python using tupletuple python iterateiterate a tuplehow to traverse a tuple in pythontraverse a tuple in pythoniterate through tuple pythonfor loop for tuple in pythonhow to iterate through a tuple using a for looppython tuple loopcan we traverse through tuple in pythonfor loop python in tuplepython iterate tupletuple iteration pythonpython iterate throuth tuplecan we loop through tuple pythonpython program to iterate over tuple using for loopsiterate to tupleiterate tupleiterate through tupleiterate through a tuple step for i in tuple pythonhow to iterate over a tuple in pythoniterrate through tuple pythoniterate tuple pythonfor loop in a tuple pythoniterating tuple pythoniterate tuple list pythoniterate a tuple pythonhow to iterate through a tuple in pythonhow to traverse tuplein pythoniterate over python tupleshow to iterate through tuples in pythontraverse tuple pythonloop a tuple pythonhow to iterate through elements in a tuple pythonpython tuple in looppython tuple iteratehow to iterate over tuple of data in pythoniterate through tuple list pythonpython iterate over tuplefastest way to iterate through tuples in pythonpython iterate through a tuplepython iterate through tuplesnamed tuple python iterate