[python] iter dict

To iterate dict common mistake what everyone will do is,

mapping = {5 : "5", 6 : "6"}
for key, val in mapping.items(): ...

Use iter* methods when possible

mapping = {5: "5", 6: "6"}
for key, val in mapping.iteritems(): ...
for key in mapping: ...

Advertisements

4 thoughts on “[python] iter dict

  1. Hi, I’m learning Python and if the mapping.items() works, why we shoul use mapping.iteritems() instead? I’m just curious.

    Thnx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s