Python: Unpacking an inner nested tuple/list while still getting its index number -
i familiar using enumerate()
:
>>> seq_flat = ('a', 'b', 'c') >>> num, entry in enumerate(seq_flat): print num, entry 0 1 b 2 c
i want able same nested list:
>>> seq_nested = (('a', 'apple'), ('b', 'boat'), ('c', 'cat'))
i can unpack with:
>>> letter, word in seq_nested: print letter, word apple b boat c cat
how should unpack following?
0 apple 1 b boat 2 c cat
the way know use counter/incrementor, un-pythonic far know. there more elegant way it?
for i, (letter, word) in enumerate(seq_nested): print i, letter, word
Comments
Post a Comment