who must makes FOR loop quicker
To pass by reference or by copy of - that is the question from hamlet. ("hamlet" - a community of people smaller than a village python3.4-linux64) xlist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] i = 0 for x in xlist: print(xlist) print("\txlist[%d] = %d" % (i, x)) if x%2 == 0 : xlist.remove(x) print(xlist, "\n\n") i = i + 1 So, catch the output and help me, PLEASE, improve the answer: Does it appropriate ALWAYS REevaluate the terms of the expression list in FOR-scope on each iteration? But if I want to pass ONCE a copy to FOR instead of a reference (as seen from an output) and reduce unreasonable reevaluation, what I must to do for that?
The iterator is not revaluated, instead, it is constructing a single iterator, in this case a list_iterator. The list_iterator looks at the underyling list to know how to iterate so when you mutate the underlying list, the list_iterator sees that. This does not mee the expression used to generate the iterator was re-evaluated. On Wed, Aug 5, 2015 at 11:25 AM, John Doe <z2911@bk.ru> wrote:
To pass by reference or by copy of - that is the question from hamlet. ("hamlet" - a community of people smaller than a village python3.4-linux64)
xlist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] i = 0 for x in xlist: print(xlist) print("\txlist[%d] = %d" % (i, x)) if x%2 == 0 : xlist.remove(x) print(xlist, "\n\n") i = i + 1
So, catch the output and help me, PLEASE, improve the answer: Does it appropriate ALWAYS REevaluate the terms of the expression list in FOR-scope on each iteration? But if I want to pass ONCE a copy to FOR instead of a reference (as seen from an output) and reduce unreasonable reevaluation, what I must to do for that? _______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/joe%40quantopian.com
On Thu, Aug 6, 2015 at 1:25 AM, John Doe <z2911@bk.ru> wrote:
To pass by reference or by copy of - that is the question from hamlet. ("hamlet" - a community of people smaller than a village python3.4-linux64)
xlist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] i = 0 for x in xlist: print(xlist) print("\txlist[%d] = %d" % (i, x)) if x%2 == 0 : xlist.remove(x) print(xlist, "\n\n") i = i + 1
So, catch the output and help me, PLEASE, improve the answer: Does it appropriate ALWAYS REevaluate the terms of the expression list in FOR-scope on each iteration? But if I want to pass ONCE a copy to FOR instead of a reference (as seen from an output) and reduce unreasonable reevaluation, what I must to do for that?
This list is for the development *of* Python, rather than development *with* Python. If you repost your question to python-list@python.org (the main user list), I'll be happy to explain over there what's going on and how to sort this out! But the simple answer is: Don't mutate the thing you're iterating over. You can take a copy with xlist[:] and iterate over that, if you like. ChrisA
On Wed, Aug 05, 2015 at 06:25:07PM +0300, John Doe wrote:
To pass by reference or by copy of - that is the question from hamlet. ("hamlet" - a community of people smaller than a village python3.4-linux64) [snip question]
John, you have already posted this same question to the tutor list, where you have been given an answer. If the response doesn't answer your question, please discuss it there on the tutor list. This question is not suitable for this list. -- Steve
participants (4)
-
Chris Angelico
-
Joe Jevnik
-
John Doe
-
Steven D'Aprano