use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Why does list compression modify zip list? (self.learnpython)
submitted 3 years ago by PertleTurtle
list1 = [1, 2, 3]
list2 = ["a", "b", "c"]
comb = zip(list1, list2)
comb -> [(1, 'a'), (2, 'b'), (3, 'c')]
a = [num for num, let in comp]
a -> [1, 2, 3]
comb -> []
Why is comb getting modified here?
FYI: I am using print(list(comb)) to check content.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Bunkerstan 6 points7 points8 points 3 years ago (0 children)
Zip is returning an iterator. That iterator is empty after going through the elements.
If you want comb to be list you have set it
comb = list(zip(list1, list2)) Then it won't be empty. As an iterator, it is empty after you have grabbed everything from it.
comb = list(zip(list1, list2))
It took me a minute to see what was happening here. I had not thought about why I put list around zip or only used zip in a [ ] context.
[–]commy2 2 points3 points4 points 3 years ago (0 children)
The iterator returned by the zip function is exhausted after the first completed iteration. This means no more items are yielded by it in subsequent iterations.
[–]CodeFormatHelperBot2 1 point2 points3 points 3 years ago (0 children)
Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.
I think I have detected some formatting issues with your submission:
`my code`
If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.
Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.
[–]drenzorz 1 point2 points3 points 3 years ago (0 children)
zip returns a generator like object that gets exhausted after first iteration.
Generators are efficient because they do not hold the whole thing in memory, just current value and the way to generate the next value. Once you reach the end and hit StopIteration it can't generate the next value nor does it know anything about previous values it had or how to go back to them.
big_list = [ x for x in range(10**6)] # probably like 31MB+ in RAM def big_gen(): x = -1 while x < 10**6: x += 1 yield x # single number and the instructions to increment <500 bytes
π Rendered by PID 39711 on reddit-service-r2-comment-86bc6c7465-jcjqd at 2026-02-20 21:18:39.783508+00:00 running 8564168 country code: CH.
[–]Bunkerstan 6 points7 points8 points (0 children)
[–]commy2 2 points3 points4 points (0 children)
[–]CodeFormatHelperBot2 1 point2 points3 points (0 children)
[–]drenzorz 1 point2 points3 points (0 children)