Posts: 17
Threads: 4
Joined: Jan 2019
May-03-2019, 02:16 PM
(This post was last modified: May-03-2019, 02:16 PM by python_user_n.)
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.
Posts: 4,220
Threads: 97
Joined: Sep 2016
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.
Posts: 4,784
Threads: 76
Joined: Jan 2018
May-03-2019, 02:32 PM
(This post was last modified: May-03-2019, 02:35 PM by Gribouillis.)
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.
Posts: 17
Threads: 4
Joined: Jan 2019
May-03-2019, 02:36 PM
(This post was last modified: May-03-2019, 02:37 PM by python_user_n.)
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)?
Posts: 4,784
Threads: 76
Joined: Jan 2018
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)]
Posts: 17
Threads: 4
Joined: Jan 2019
May-03-2019, 02:44 PM
(This post was last modified: May-03-2019, 02:44 PM by python_user_n.)
(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.
Posts: 4,784
Threads: 76
Joined: Jan 2018
May-03-2019, 02:52 PM
(This post was last modified: May-03-2019, 02:52 PM by Gribouillis.)
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.
|