Home > Articles > Open Source > Python

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Iterables Everywhere

Another theme of 3.x is memory conservation. Iterators are much more efficient than having entire lists in memory, especially when the target action on the objects in question is iteration. There's no need to waste memory when it's not necessary. What you'll notice primarily is that code that previously returned lists no longer does so. For example, the following functions all return some sort of iterator instead:

  • map()
  • filter()
  • range()
  • zip()

The same is true for these dictionary methods:

  • keys()
  • items()
  • values()

This approach may be more inconvenient if you want to glance at your data, but it's better in terms of resource consumption. The changes are mostly under the covers; if you only use their return values to iterate over, you won't notice a thing!

  • + Share This
  • 🔖 Save To Your Account