Question 1
how to make an iterable object like this:
0 1 2 3 4 5 4 3 2 1 0 1 2 3 4 5 4 3 2 1 0 1 2 3 4 5 4 3 2 1 ....
Question 2
if use list(obj) on the object above would this eat up machine’s memory? how to prevent it?
Please don’t use python2
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can make an infinite generator that counts up and down:
would output:
You cannot prevent
list(updown(6))from trying to consume all memory, no. As the doctor would say: “Then don’t do that!”.Use
.next()calls instead, or use your generator with another statement that limits the number of times you iterate over the generator. Theitertools.islice()function would do just that: