[Tutor] who makes FOR loop quicker

John Doe z2911 at bk.ru
Thu Aug 6 10:34:51 CEST 2015


Can You, please, elaborate this "..Passing in Python is different than 
in C or other languages..."

'Cause as far as I know - default major Python's implementation CPython 
is written in C.



> Joel Goldstick 於 08/05/2015 03:44 PM 寫道:
>> On Wed, Aug 5, 2015 at 3:53 AM, John Doe <z2911 at 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, PLEASE, me improve the answer:
>>> Does it appropriate ALWAYS reevaluate the terms of the list on each
>>> iteration?
>>> But if I want to pass a copy to FOR instead of a reference (as seen
>>> from an
>>> output) and reduce unreasonable reevaluation, what I must to do for
>>> that?
>>
>> You aren't passing anything.  the for statement is in the same
>> namespace as the rest of the code.  Passing in python is different
>> than in C or other languages.
>>
>> A couple of comments:
>>
>> setting i = 0, then incrementing at the end of the loop would more
>> pythonically be done with the enumerate function.
>> Its generally a bad idea to remove items from and iterable while
>> interating over it.  I'm guessing that this is what is confusing you.
>> One way to remove items from a list is to create a new list, and
>> append items you want to it, skipping the ones you don't.  You don't
>> really need the index at all since python interation protocol will
>> walk through the list for you without worrying about index values
>>


More information about the Tutor mailing list