Alexey Vyskubov’s Post

Some fun with Python iterators. Let's take some list >>> lst = [1, 2, 3, 4] and build an iterator out of it >>> lst_i = iter(lst) Now I have an iterator over lst, so it should produce elements 1, 2, 3, 4. And indeed, the first element is 1. >>> list(zip(lst_i, ['a'])) [(1, 'a')] But let's see what is left... >>> list(zip(lst_i, ['b', 'c', 'd'])) [(3, 'b'), (4, 'c')] Where did 2 go? Well, the explanation is as following. The first zip gets 1 from lst_i and 'a' from ['a']. Then it gets 2 from lst_i and nothing from ['a'], so 2 is **discarded**. What is left for the second zip is 3 and 4. #Python #gotcha

Moral of the story: zip(x, y) does not do the same thing as zip(y, x).

Pavel Perikov do you remember that infamous discussion with some guy who was claiming that Python is very easy, and then banned you and deleted all my (much less sofisticated) examples of "interesting" things?

On the one hand, Python is much harder to learn compared to Haskell / Idris. On the other hand, once you learn it it's also harder to use. At least the resulting code is lower quality and less efficient as well!

See more comments

To view or add a comment, sign in

Explore content categories