things = {}
things[1] = (2,3)
things[2] = (4,5)
for id, (A, B) in things.items():
print id, A, B
I could be wrong, but I vaguely recall trying this once, and it not working. Now it does!
things = {}
things[1] = (2,3)
things[2] = (4,5)
for id, (A, B) in things.items():
print id, A, B
6 comments:
Works for years like this... ;)
And you can use the new unpacking sintax available in Python 3.x
things = {}
things[0] = (1,2,3)
for id, (a, *b) in things.items():
print(id, a, b)
other variations:
- explicitly ignore an arg using the "_" variable. This is most useful when you're using the 2nd and 5th args inside a packed row of data.
for _, (a,b) in things.items():
dostuff
Since you're not using the keys, don't ask for them. Also itervalues() fetches one item at a time, vs making a huge list.
for (a,b) in things.itervalues():
dostuff
I just tested this under Python 1.5 and it worked perfectly. Just how long ago did you try this and find that it did not work? :)
LOL, please note these words from my post, "Vaguely" and "Could be wrong" :-)
imo people should use that a lot more because otherwise they do arcane workarounds to get the same resuls; more examples: http://goo.gl/jyrIn
Post a Comment