7

I was wondering what the difference is between the zip() function in python 2 and python 3 is. I noticed when using the timeit module on both functions that the python 3 function was a lot faster. Thanks so much in advance :)

  • 1
    One thing is that in *Python3*, it returns a *generator*. – CristiFati Apr 10 '18 at 06:17
  • Python 2's `zip` builds and returns a list. Python 3's `zip` returns a lazy iterator. If you actually iterate over both of them, Python 3's will save memory but have the same effect. But if you never try to use the values, Python 3 will be even smarter and just never generate them, which is of course amazingly fast, but not that useful. – abarnert Apr 10 '18 at 06:19

2 Answers2

6

Difference between Python 2 and Python 3 is Python 3 returns an iterators. Idea of this saving memory.

Axis
  • 2,066
  • 2
  • 21
  • 40
2

In Python 3, the zip() function returns an iterator, meaning that you can only exhaust elements once, whereas Python 2 returns an iterable itself.

see here: Python 2 Doc, Python 3 Doc

rammelmueller
  • 1,092
  • 1
  • 14
  • 29