Python Forum
Self re-initializing variable?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Self re-initializing variable?
#1
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.
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Reply
#4
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)?
Reply
#5
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)]
Reply
#6
(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.
Reply
#7
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Auto-py-to-exe Stuck At 'Initializing' killingtime 5 6,940 Jan-21-2024, 10:52 PM
Last Post: ShiDari
  "unexpected keyword arg" when initializing my subclasses Phaze90 3 3,111 Nov-25-2022, 07:39 PM
Last Post: Gribouillis
  Initializing a PRINTDLG structure-how to hammer 4 1,483 Jun-08-2022, 07:07 PM
Last Post: jefsummers
  Syntax when initializing empty lists Mark17 2 1,355 Jun-02-2022, 04:09 PM
Last Post: Mark17
  Initializing, reading and updating a large JSON file medatib531 0 1,767 Mar-10-2022, 07:58 PM
Last Post: medatib531

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020