Python Forum
Self re-initializing variable? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Self re-initializing variable? (/thread-18038.html)



Self re-initializing variable? - python_user_n - May-03-2019

Hello,
I'm testing this snippet of code:
ld = [1, 2, 3]
ll = ['a', 'b', 'c']
loft = zip(ll, ld)
#for i in loft:
#    print(i)
print(loft)
d = dict(loft)
print(d)
If I uncomment the for loop the dictionary will be empty. I suppose for some reason loft reinitialize itself as empty variable.
I want to know if it's a normal behavior or my poor understanding of the language specifications.
Thanks.


RE: Self re-initializing variable? - ichabod801 - May-03-2019

What is happening is that loft is an iterator. Iterators can only be gone through once. If you go through it in the for loop, the dict can't go through it. You would need to either recreate it with the zip command, or use list or tuple to convert it to an iterable, which can create multiple iterators and thus be gone through multiple times.


RE: Self re-initializing variable? - Gribouillis - May-03-2019

In the for statement
for item in FOO:
    do_something()
Python expects FOO to be an iterable. By default, an iterable can be traversed once, then it is exhausted. It's like a pack of cookies, you can eat then only once until the pack is empty. This is what happens with the zip object. If you want an iterable that can be traversed more than once, use a list
loft = list(zip(ll, ld))
The difference is that the list instance keeps pointers to all the items in memory while the zip object instance does not. Python's default behavior saves memory in many cases.


RE: Self re-initializing variable? - python_user_n - May-03-2019

Thanks.
I've just tried to use list(zip(...)) and it worked.
Is there another way to create a list of tuples beside zip()?
And the second question: zip() return an iterator? So I can return this iterator to the initial state? Or it do not remember it's initial state (reference)?


RE: Self re-initializing variable? - Gribouillis - May-03-2019

Quote:If there another way to create a list of tuples beside zip()?
There are many ways. One of them is list comprehension
>>> [(i, i+1, i+2) for i in range(5)]
[(0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6)]



RE: Self re-initializing variable? - python_user_n - May-03-2019

(May-03-2019, 02:38 PM)Gribouillis Wrote:
Quote:If there another way to create a list of tuples beside zip()?
There are many ways. One of them is list comprehension
>>> [(i, i+1, i+2) for i in range(5)]
[(0, 1, 2), (1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6)]

Very concise method for creating list of values.
And if I need to combine already pre-existing values from one list/tuples to another list of tuples?
Thanks.


RE: Self re-initializing variable? - Gribouillis - May-03-2019

python_user_n Wrote:Or it do not remember it's initial state (reference)?
The zip object does not remember its initial state. I can give you Cpython's source code as a reference. If you look carefully, you'll see that the zip object stores a tuple of iterators that iterate on each of the zip's arguments. These iterators may not remember their initial state, and the zip object never tries to reinitialize them.